问题概述:
PHP 的simplexml_load_string() 无法解析 SOAP XML 响应并检索
解决此问题的一种有效方法是在使用 simplexml_load_string() 之前从 XML 响应中删除命名空间前缀。操作方法如下:
$your_xml_response = '<soap xml here>'; $clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $your_xml_response); $xml = simplexml_load_string($clean_xml);</soap>
对于提供的 SOAP XML 响应:
<envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <body> <paymentnotification xmlns="http://apilistener.envoyservices.com"> <payment> <uniquereference>ESDEUR11039872</uniquereference> <epacsreference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsreference> <postingdate>2010-11-15T15:19:45</postingdate> <bankcurrency>EUR</bankcurrency> <bankamount>1.00</bankamount> <appliedcurrency>EUR</appliedcurrency> <appliedamount>1.00</appliedamount> <countrycode>ES</countrycode> <bankinformation>Sean Wood</bankinformation> <merchantreference>ESDEUR11039872</merchantreference> </payment> </paymentnotification> </body> </envelope>
执行代码:
$your_xml_response = '<!-- Your SOAP XML -->'; $clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $your_xml_response); $xml = simplexml_load_string($clean_xml); foreach ($xml->Body->PaymentNotification->payment as $item) { print_r($item); }
这会将“付款”元素输出为 SimpleXMLElement 对象,提供对其子元素的访问。
以上是如何使用 PHP 从 SOAP XML 响应中检索' payment ”元素?的详细内容。更多信息请关注PHP中文网其他相关文章!