【问题标题】:Cant connect to webservice with simple php test script无法使用简单的 php 测试脚本连接到 web 服务
【发布时间】:2011-10-14 15:55:41
【问题描述】:

我正在尝试连接到这个网络服务:http://magnetledavet.com/GetStuffed/Service.asmx?WSDL

我正在使用这个简单的脚本来测试它,但我似乎没有得到任何回报,而且我不确定是什么导致了问题。

这是我的测试脚本,任何人都可以看到有什么问题吗?

<?php

if(!class_exists("SoapClient")){ die("Catch"); }


$client = new SoapClient('http://magnetledavet.com/GetStuffed/Service.asmx?WSDL',     array('soap_version'  => SOAP_1_2));
echo "test";
//var_dump($client->__getFunctions());


echo("Dump Start:<br>");
var_dump($client->SendOrder( array(
                               "merchant_id"    => 1, 
                               "order_id"       => "445",
                               "username"       => "test",
                               "password"       => "test123",
                               "products"       => array( 
                                                    array("qty" => 1, "product_id" =>"1", "product_name" => "Product Test 1", "price" =>15), 
                                                    array("qty" => 1, "product_id" =>"2", "product_name" => "Product Test 2", "price" =>25)
                                                        ),
                               "order_details"  => "Dont add cheese !!",
                               "amount"         => 40,
                               "point"          => 40,
                               "payment_type"   => "test",
                               "webdate"        => date("Y-m-d").'T'.date("H:i:s"),
                               "customer_name"  => "Steve Jobs",
                               "address"        => "4th Floor Grosvenor House, 1 High Street Edgware",
                               "state"          => "test",
                               "phone"          => "+447711111111",
                               "postcode"       => "HA8 7TA",
                               "email"          => "abc@abc.com",
                               "delivery_time"  => date("Y-m-d").'T'.date("H:i:s"),
                               "delivery_notes" => "test"
                               ) 
                        ));
//var_dump($client->__soapCall("SendOrder", array( 'merchant_id' => 1 )));
print("Dump End:<br>");

 ?>

得到以下soap错误:SOAP-ERROR: Parsing WSDL: Couldn't load from 'magnetledavet.com/GetStuffed/Service.asmx?WSDL'; :未能加载外部实体“magnetledavet.com/GetStuffed/Service.asmx?WSDL”;在 C:\xampp\htdocs\get_stuffed\webservice.php:6 堆栈跟踪:#0 C:\xampp\htdocs\get_stuffed\webservice.php(6): SoapClient->SoapClient('magnetle...';, Array ) #1 {main} 抛出 C:\xampp\htdocs\get_stuffed\webservice.php

【问题讨论】:

  • 您是否遇到任何错误?变量转储显示什么?
  • 根本没有显示没有错误或没有转储。
  • SOAP 客户端肯定有一些调试选项吗? php.net/manual/en/soapclient.soapcall.phpOn error, a call to a SOAP function can cause PHP to throw exceptions or return a SoapFault object if exceptions are disabled. To check if the function call failed to catch the SoapFault exceptions, check the result with is_soap_fault().
  • 这是我第一次使用 php soap,所以我不太确定。此行之后的所有内容,甚至 echo test $client = new SoapClient('magnetledavet.com/GetStuffed/Service.asmx?WSDL', array('soap_version' => SOAP_1_2));
  • 是否启用错误报告? error_reporting(E_ALL);

标签: php web-services soap


【解决方案1】:

代码是正确的。问题在于提供 WSDL 文件的 Web 服务器。事实证明,如果您提供 user-agentkeep-alive 标头,它不会发送给您。我认为这不能通过 SoapClient 设置。但是您仍然可以手动下载 wsdl。我创建了一个脚本,它将每小时通过 cURL 下载一次 WSDL 文件。 SoapClient 将使用本地缓存的文件。

if(!is_file('test.wsdl') || filemtime('test.wsdl') < time()-3600) {

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://magnetledavet.com/GetStuffed/Service.asmx?WSDL");
  $header = array('User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20100101 Firefox/6.0'
  , 'Connection: keep-alive');
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  $wsdl = curl_exec($ch);
  file_put_contents('test.wsdl', $wsdl);
}

$client = new SoapClient('test.wsdl',     array('soap_version'  => SOAP_1_2));

尝试删除用户代理,看看没有它就无法检索 WSDL 文件。

【讨论】:

  • 嗯,现在似乎可以使用我的原始代码了。一定是网络服务本身的问题。
猜你喜欢
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
  • 2013-02-04
  • 1970-01-01
  • 1970-01-01
  • 2014-04-02
  • 2014-06-21
  • 2018-06-10
相关资源
最近更新 更多