微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐