I'm using cURL to send an request to a SOAP service, I send in POST Body the XML containing parameters, in response I receive:
我正在使用cURL向SOAP服务发送请求,我在POST Body中发送包含参数的XML,作为响应,我收到:
Web service: http://lcbtestxmlv2.ivector.co.uk/soap/book.asmx?WSDL
Web服务:http://lcbtestxmlv2.ivector.co.uk/soap/book.asmx?WSDL
true
http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5®iOnid=9
215
1795
Hotel Gaddis
3.0
Egypte
Louxor
Cet établissement confortable propose un très bon service à un bon rapport qualité-prix. Cet hôtel de 6 étages compte 55 chambres et comprend une terrasse, une réception avec coffre-fort et ascenseur,
Cet établissement confortable propose un très bon service à un bon rapport qualité-prix. Cet hôtel de 6 étages compte 55 chambres et comprend une terrasse, une réception avec coffre-fort et ascenseur,...
http://lcbtestxml1.ivector.co.uk/content/DataObjects/Property/Image/
image_1795_v1.jpg
imagethumb_1795_v1.jpg
http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5®iOnid=9&propertyid=1795
1
690039000
3
Twin/double Room
Petit Déjeuner
false
150.58
0
150.58
2
1
0
1
690039001
7
Twin/double Room
Demi-Pension
false
291.64
0
291.64
2
1
0
1
690039002
5
Double/twin Room
Pension Complète
false
529.22
0
529.22
2
1
0
I don't have enough experience with XML data. I spent hours trying to convert the XML response to a PHP object or array, but without any success.
我没有足够的XML数据经验。我花了几个小时尝试将XML响应转换为PHP对象或数组,但没有任何成功。
I need to read all PropertyResults.
我需要阅读所有PropertyResults。
PHP Code:
PHP代码:
$xml = simplexml_load_string($soap_xml_result);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('xsd', 'http://www.w3.org/2001/XMLSchema');
$test = (string) $xml->Body->SearchResponse->SearchResult->SearchURL;
var_export($test);
9
The hint of bksi is not that wrong, however technically as this is XML you only need to access the namespaced elements properly. This works more easy by using an XPath expression and registering the namspace-uri to your own prefix:
bksi的提示并没有错,但从技术上来说,这是XML,你只需要正确访问命名空间元素。通过使用XPath表达式并将namspace-uri注册到您自己的前缀,这可以更轻松地工作:
$soap = simplexml_load_string($soapXMLResult);
$soap->registerXPathNamespace('ns1', 'http://ivectorbookingxml/');
$test = (string) $soap->xpath('//ns1:SearchResponse/ns1:SearchResult/ns1:SearchURL[1]')[0];
var_dump($test);
Output:
输出:
string(100) "http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5®iOnid=9"
If you don't want to use XPath, you need to specify the namespace while you traverse, only the children in the namespace of the element itself are available directly if the element itself is not prefixed. As the root element is prefixed you first need to traverse up to the response:
如果您不想使用XPath,则需要在遍历时指定命名空间,如果元素本身没有前缀,则只有元素本身的命名空间中的子项可以直接使用。由于根元素是前缀的,因此首先需要遍历响应:
$soap = simplexml_load_string($soapXMLResult);
$respOnse= $soap->children('http://schemas.xmlsoap.org/soap/envelope/')
->Body->children()
->SearchResponse
;
Then you can make use of the $response
variable as you know it:
然后你可以使用你知道的$ response变量:
$test = (string) $response->SearchResult->SearchURL;
because that element is not prefixed. As a more complex result is returned this is probably the best because you can easily access all the response values.
因为该元素没有前缀。返回更复杂的结果时,这可能是最好的,因为您可以轻松访问所有响应值。
Your question is similar to:
您的问题类似于:
Maybe the code/descriptions there are helpful, too.
也许代码/描述也有帮助。
9
You could consider passing the SOAP response through a DOM Document, and then converting it into a simplexml object.
您可以考虑通过DOM Document传递SOAP响应,然后将其转换为simplexml对象。
loadHTML($soap_response);
libxml_clear_errors();
$xml = $doc->saveXML($doc->documentElement);
$xml = simplexml_load_string($xml);
$respOnse= $xml->body->envelope->body->searchresponse;
//print_r($response); exit;
echo $response->searchresult->returnstatus->success;
echo '
';
echo $response->searchresult->searchurl;
?>
However, this may cause problems with special characters in your response, like é and à. Else, it works.
但是,这可能会导致您的回复中出现特殊字符问题,例如é和à。否则,它的工作原理。
2
Hm. You should use SOAP client to do that, not just send SOAP requests. PHP has integrated SOAP functionalities http://php.net/manual/en/book.soap.php.
嗯。您应该使用SOAP客户端来执行此操作,而不仅仅是发送SOAP请求。 PHP集成了SOAP功能http://php.net/manual/en/book.soap.php。
There are custom soap libraries like NuSOAP http://sourceforge.net/projects/nusoap/.
有像NuSOAP http://sourceforge.net/projects/nusoap/这样的自定义肥皂库。
Most of php frameworks has SOAP libraries too.
大多数php框架也都有SOAP库。
2
Another solution, the only solution that worked for me :
另一种解决方案,唯一对我有用的解决方案:
$xml = $soap_xml_result;
$xml = preg_replace("/(<\/?)(\w+):([^>]*>)/", '$1$2$3', $xml);
$xml = simplexml_load_string($xml);
$json = json_encode($xml);
$respOnseArray= json_decode($json, true); // true to have an array, false for an object
print_r($responseArray);
Enjoy :)
请享用 :)