[英]how to traverse soap response with curl request in php?
如何在php中获取soap响应的标签内的值。响应是这样的。
string '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetListResponse xmlns="http://test.org/">
<GetListResult>[{"Id":30,"Name":"OFFICE"},{"Id":31,"Name":"KUMAR KHATRI"},{"Id":32,"Name":"ASHA MAIYA SHRESTHA"},{"Id":33,"Name":"RABINDRA GHIMIRE"},{"Id":34,"Name":"CHABBI GHIMIRE"},{"Id":35,"Name":"RAJ KUMAR SHRESTHA"},{"Id":36,"Name":"RABINDRA BDH. RO'... (length=614)
要获取GetListResult
的值,您可以这样做:
$source = <<<EOS
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetListResponse xmlns="http://test.org/">
<GetListResult>
[{"Id":30,"Name":"OFFICE"},{"Id":31,"Name":"KUMAR KHATRI"},{"Id":32,"Name":"ASHA MAIYA SHRESTHA"},{"Id":33,"Name":"RABINDRA GHIMIRE"},{"Id":34,"Name":"CHABBI GHIMIRE"},{"Id":35,"Name":"RAJ KUMAR SHRESTHA"}]
</GetListResult>
</GetListResponse>
</soap:Body>
</soap:Envelope>
EOS;
// Create simple XML element
$xml = new SimpleXMLElement($source);
$xml->registerXPathNamespace('test', 'http://test.org/');
// Get value of first "GetListResponse" element
$result = (string)$xml->xpath('//test:GetListResult')[0];
// Parse JSON
$values = json_decode($result, true);
var_dump($values);
输出:
array(6) {
[0]=>
array(2) {
["Id"]=>
int(30)
["Name"]=>
string(6) "OFFICE"
}
[1]=>
array(2) {
["Id"]=>
int(31)
["Name"]=>
string(12) "KUMAR KHATRI"
}
...
}
PHP本身还集成了一些SOAP功能 。 如果可以利用它,则可以完全删除CURL + SimpleXMLElement的使用。 像这样(未经测试):
$soapClient = new SoapClient("http://test.org/wsdl?WSDL");
$soapResult = $soapClient->SomeFunction(array('foo'=>'bar', 'baz'=>'fez'));
$result = $soapResult->GetListResponse->GetListResult;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.