"Unable to parse URL" SOAP 请求后出现异常
"Unable to parse URL" exception after SOAP request
我正在尝试将 soap 服务与 php 一起使用,但该服务似乎无法解释 php 创建的请求格式。到目前为止我有一个简单的例子:
ini_set("soap.wsdl_cache_enabled", "0");
$client = new SoapClient('http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc?WSDL',array("trace"=>1));
try {
$response = $client->GetWebservicesVersion();
} catch(Exception $e){
print_r($e);
}
输出:
SoapFault Object
(
[message:protected] => Unable to parse URL
[string:Exception:private] =>
[code:protected] => 0
[file:protected] => /var/www/test.php
[line:protected] => 6
[trace:Exception:private] => Array
(
[0] => Array
(
[function] => __doRequest
[class] => SoapClient
[type] => ->
[args] => Array
(
[0] => <?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://webservices.hotel.de/V2_8/"><SOAP-ENV:Body><ns1:GetWebservicesVersion/></SOAP-ENV:Body></SOAP-ENV:Envelope>
[1] =>
[2] => http://webservices.hotel.de/V2_8/IBaseService/GetWebservicesVersion
[3] => 1
[4] => 0
)
)
[1] => Array
(
[file] => /var/www/test.php
[line] => 6
[function] => __call
[class] => SoapClient
[type] => ->
[args] => Array
(
[0] => GetWebservicesVersion
[1] => Array
(
)
)
)
[2] => Array
(
[file] => /var/www/test.php
[line] => 6
[function] => GetWebservicesVersion
[class] => SoapClient
[type] => ->
[args] => Array
(
)
)
)
[previous:Exception:private] =>
[faultstring] => Unable to parse URL
[faultcode] => HTTP
)
生成的请求 xml 是:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://webservices.hotel.de/V2_8/">
<SOAP-ENV:Body>
<ns1:GetWebservicesVersion />
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
但是根据 web 服务的文档,它期望:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetWebservicesVersion xmlns="http://webservices.hotel.de/V2_8/"/>
</soap:Body>
</soap:Envelope>
有没有办法让请求 xml 达到预期的格式?
您的最终请求没有问题(等同于文档中的预期请求)。问题是 wsdl http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc?WSDL
中没有指定端点地址。所以 SoapClient 不知道 URL 到哪里去 post 请求,你会得到错误 Unable to parse URL
。您必须在代码中手动指定端点 URL。对于 SoapClient 这应该有效:
$client = new SoapClient('http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc?WSDL',array("trace"=>1));
$client->__setLocation('http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc');
也检查此 post。
请求已成功处理。
try {
if(setEndpoint('changeEndpoint')){
$newLocation = $client->__setLocation(setEndpoint(' https://wsbeta.fedex.com:443/web-services')); // Please set url fedex email response
}
/* echo "<pre>";
Print_r($request); */
$response = $client ->track($request);
}
我正在尝试将 soap 服务与 php 一起使用,但该服务似乎无法解释 php 创建的请求格式。到目前为止我有一个简单的例子:
ini_set("soap.wsdl_cache_enabled", "0");
$client = new SoapClient('http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc?WSDL',array("trace"=>1));
try {
$response = $client->GetWebservicesVersion();
} catch(Exception $e){
print_r($e);
}
输出:
SoapFault Object
(
[message:protected] => Unable to parse URL
[string:Exception:private] =>
[code:protected] => 0
[file:protected] => /var/www/test.php
[line:protected] => 6
[trace:Exception:private] => Array
(
[0] => Array
(
[function] => __doRequest
[class] => SoapClient
[type] => ->
[args] => Array
(
[0] => <?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://webservices.hotel.de/V2_8/"><SOAP-ENV:Body><ns1:GetWebservicesVersion/></SOAP-ENV:Body></SOAP-ENV:Envelope>
[1] =>
[2] => http://webservices.hotel.de/V2_8/IBaseService/GetWebservicesVersion
[3] => 1
[4] => 0
)
)
[1] => Array
(
[file] => /var/www/test.php
[line] => 6
[function] => __call
[class] => SoapClient
[type] => ->
[args] => Array
(
[0] => GetWebservicesVersion
[1] => Array
(
)
)
)
[2] => Array
(
[file] => /var/www/test.php
[line] => 6
[function] => GetWebservicesVersion
[class] => SoapClient
[type] => ->
[args] => Array
(
)
)
)
[previous:Exception:private] =>
[faultstring] => Unable to parse URL
[faultcode] => HTTP
)
生成的请求 xml 是:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://webservices.hotel.de/V2_8/">
<SOAP-ENV:Body>
<ns1:GetWebservicesVersion />
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
但是根据 web 服务的文档,它期望:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetWebservicesVersion xmlns="http://webservices.hotel.de/V2_8/"/>
</soap:Body>
</soap:Envelope>
有没有办法让请求 xml 达到预期的格式?
您的最终请求没有问题(等同于文档中的预期请求)。问题是 wsdl http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc?WSDL
中没有指定端点地址。所以 SoapClient 不知道 URL 到哪里去 post 请求,你会得到错误 Unable to parse URL
。您必须在代码中手动指定端点 URL。对于 SoapClient 这应该有效:
$client = new SoapClient('http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc?WSDL',array("trace"=>1));
$client->__setLocation('http://publicbetawebservices.hotel.de/V2_8/FreeHotelSearchWebService.svc');
也检查此 post。
请求已成功处理。
try {
if(setEndpoint('changeEndpoint')){
$newLocation = $client->__setLocation(setEndpoint(' https://wsbeta.fedex.com:443/web-services')); // Please set url fedex email response
}
/* echo "<pre>";
Print_r($request); */
$response = $client ->track($request);
}