如何通过PHP读取SOAP回复包络

前端之家收集整理的这篇文章主要介绍了如何通过PHP读取SOAP回复包络前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何从SOAP回复信封中读取error_code?我的 PHP版本是:5.2.0.
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
 <soap:Body>
  <Response xmlns="http://xxx.gateway.xxx.abcd.com">
   <return>
      <transaction_id>1234567</transaction_id>
      <error_code>109</error_code>    
   </return>
  </Response>
 </soap:Body>
</soap:Envelope>

我只需要读取error_code tag的值.其值为:109

我正在使用nusoap.我使用下面的代码但没有正常工作:

$response=htmlspecialchars($client->response,ENT_QUOTES);
$xml = simplexml_load_string($response); 
$ns = $xml->getNamespaces(true); 
$soap = $xml->children($ns['soap']); 
$error_code = $soap->body->children($ns['error_code']);
<?PHP 

$string = <<<XML
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
 <soap:Body>
  <Response xmlns="http://xxx.gateway.xxx.abcd.com">
   <return>
      <transaction_id>1234567</transaction_id>
      <error_code>109</error_code>    
   </return>
  </Response>
 </soap:Body>
</soap:Envelope>
XML;

$xml = new SimpleXMLElement($string); 
$xml->registerXPathNamespace("soap","http://www.w3.org/2003/05/soap-envelope");
$body = $xml->xpath("//soap:Body");
$error_code = (string)$body[0]->Response->return->error_code;
print_r($error_code); 
?>

要么

$xml = simplexml_load_string($string); 
 $error_code = (string)$xml->children('soap',true)
                            ->Body
                            ->children()
                            ->Response
                            ->return
                            ->error_code;

猜你在找的PHP相关文章