无法使用简单的php测试脚本连接到Web服务

时间:2011-10-14 15:55:41

标签: php web-services soap

我正在尝试连接到此网络服务: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:解析WSDL:无法从'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-&gt; SoapClient('magnetle ...';,数组)#1 {main}抛出C:\ xampp \ htdocs \ get_stuffed \ webservice.php

1 个答案:

答案 0 :(得分:0)

代码是正确的。问题在于服务于WSDL文件的Web服务器。事实证明,如果您向下提供user-agentkeep-alive标头,则不会将其发送给您。我认为这不能通过SoapClient设置。但你仍然可以下载wsdl manualy。我创建了一个脚本,它将每小时通过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文件。