![](/img/trans.png)
[英]soap error Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host
[英]php soap client, error from bad wsdl? “Uncaught SoapFault exception: [HTTP] Could not connect to host”
我主要是SOAP的新手,所以我做了一些测试脚本来连接到客户的服务器。 它们中有一个GetMessage命令,不需要输入或身份验证,仅用于测试连接性:
<?php
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$url = "https://test.mycustomer.com/api/TestService.svc?wsdl";
$client = new SoapClient($url, array("trace" => 1, "exception" => 0));
$result = $client->GetMessage(NULL);
echo "<pre>".print_r($result, true)."</pre>";
if($result->GetMessageResult->Status == "Success")
{
echo "Item deleted!";
}
?>
如果我在命令行中运行此命令,则会得到:
Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host in /var/www/my.stage.com/htdocs/common/soaptest.php:8
Stack trace:
#0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'http://459265-d...', 'http://tempuri....', 1, 0)
#1 [internal function]: SoapClient->__call('GetMessage', Array)
#2 /var/www/my.stage.com/htdocs/common/soaptest.php(8): SoapClient->GetMessage(NULL)
#3 {main}
thrown in /var/www/my.stage.com/htdocs/common/soaptest.php on line 8
从浏览器中我得到:
PHP Notice: 'SoapClient::__doRequest() [soapclient.--dorequest]: php_network_getaddresses: getaddrinfo failed: Name or service not known'
在此服务的WSDL中,字符串“ 459265”显示在此处:
<wsdl:service name="TestService">
<wsdl:port name="BasicHttpBinding_ITestService" binding="tns:BasicHttpBinding_ITestService">
<soap:address location="http://459265-dev1/api/TestService.svc"/>
</wsdl:port>
<wsdl:port name="BasicHttpsBinding_ITestService" binding="tns:BasicHttpsBinding_ITestService">
<soap:address location="https://test.mycustomer.com/api/TestService.svc"/>
</wsdl:port>
</wsdl:service>
所以我的问题是,对吗? WSDL是否应具有无法从框访问的本地URL?
更多信息,当我在__getFunctions和__getTypes上执行var_dump时,我得到
array(2) {
[0]=>
string(53) "GetMessageResponse GetMessage(GetMessage $parameters)"
[1]=>
string(53) "GetMessageResponse GetMessage(GetMessage $parameters)"
}
array(5) {
[0]=>
string(21) "struct GetMessage {
}"
[1]=>
string(55) "struct GetMessageResponse {
string GetMessageResult;
}"
[2]=>
string(8) "int char"
[3]=>
string(17) "duration duration"
[4]=>
string(11) "string guid"
}
您要做的是将代码包装在try{}, catch{}
块中。 例如,
<?php
try {
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);
$url = "https://test.mycustomer.com/api/TestService.asmx?wsdl";
$client = new SoapClient($url, array("trace" => 1, "exception" => 0));
$result = $client->GetMessage(NULL);
echo "<pre>".print_r($result, true)."</pre>";
if($result->GetMessageResult->Status == "Success")
{
echo "Item deleted!";
}
}
catch (SoapFault $exception) {
echo $exception->getMessage();
}
?>
如错误所述, Fatal error: Uncaught SoapFault exception: [HTTP] Could not connect to host in ....
,因此无论如何您都需要捕获该异常。 希望这可以帮助。
部分解决。 问题出在wsdl服务声明中,其中有两个用于相同绑定的端口(请参见上文)。 在php的SoapClient中,__doRequest的$ location来自第一个绑定(如果我使用了错误的术语,请原谅我),并且该URL的地址是我无法访问的本地URL。 所以我这样做:
class MySoapClient extends SoapClient
{
public function __doRequest($request, $location, $action, $version, $one_way = 0)
{
$response = parent::__doRequest($request, "https://test.mycustomer.com/api/TestService.svc", $action, $version, $one_way);
return($response);
}
}
扩展SoapClient并覆盖__doRequest,我能够将$ location设置为我知道可以使用的url。 再次运行并收到欢迎消息-是! 这不是一个很好的解决方案,因为我必须为每个服务扩展一个新客户端,但是要解决此问题,我要做的就是取出硬编码的url,将$ location中的本地域替换为外部域,然后调用__doRequest,它将与所有服务一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.