首页 > 解决方案 > 使用 PHP 从 SOAP 响应中获取价值

问题描述

我正在尝试从下面的肥皂响应中获取“ErrorDetail”,但对我不起作用。我的反应很好。我试过这个。

 $response = curl_exec($ch);
   $response_xml = str_replace("<SOAP-ENV:Body>","", $response);
   $response_xml = str_replace("</SOAP-ENV:Body>","", $response_xml);
   $return = simplexml_load_string($response_xml);
   $value = (string) $return->lodgeWithCertificateResponseType->Notification->Id;
   $status = (string) $return->lodgeWithCertificateResponseType->Notification->Status;
   $error = (string) $return->detail->fault->ErrorMessage;

肥皂反应是

string(617) "<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring xml:lang="en">Validation error</faultstring>
<detail>
<fault xmlns:ns3="http://bdm.nsw.gov.au/Schemas/CommonTypes_v1.0" xmlns:ns4="http://bdm.nsw.gov.au/Schemas/StatusQuery_v1.0">
<ErrorMessage>Validation error</ErrorMessage>
<ErrorDetail>Deceased's family name at birth must be in capitals</ErrorDetail>
<ErrorDetail>Deceased's Family Name must be in capitals</ErrorDetail>
</fault>
</detail>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>"
object(SimpleXMLElement)#1 (0) {
}

任何帮助将不胜感激

标签: phpxmlsoap

解决方案


尝试这样的事情:

$string = <<<XML
[your SOAP response above]
XML;

$xml = simplexml_load_string($string);
$results = $xml->xpath('//fault/ErrorDetail');
$err = $results[0]->xpath('./preceding-sibling::ErrorMessage/text()');   
echo $err[0]."\r\n";
foreach($results as $result) {
   echo $result;
   echo "\r\n";
}

输出:

Validation error 
Deceased's family name at birth must be in capitals
Deceased's Family Name must be in capitals

推荐阅读