简体   繁体   English

以XML显示cURL响应

[英]Show cURL response in XML

I am getting a response of SOAP API, and the response have header, body, status etc. I want to get the status from the response. 我正在获取SOAP API的响应,并且响应具有标头,正文,状态等。我想从响应中获取状态。 Though response is in xml format, when I echo the response, it is showing on one line without xml tags. 尽管响应是xml格式,但是当我回显响应时,它显示为没有xml标签的一行。 When I use below, it shows with xml format: 当我在下面使用时,它以xml格式显示:

echo '<pre>';
echo htmlspecialchars(print_r($response, true));
echo '</pre>';

Now I want to get Status from this xml. 现在,我想从此xml获取状态。 I am doing it as below, but not getting anything 我正在做如下,但什么也没得到

$oXML = new SimpleXMLElement($response);
foreach($oXML as $oEntry){
    echo $oEntry->status . "\n";echo "in foreach";
if ($oEntry->status == "Failure"){echo "Failed";}
else echo "success";
}

And like this as well 就像这样

$xml = simplexml_load_string($response); 
print_r($xml); 

How to get the status? 如何获得身份? Status is 'Success' or 'failure' 状态为“成功”或“失败”

xml response is: xml响应是:

 <?xml version='1.0' encoding='utf-8'?><soapenv:Envelope....>
    <soapenv:Header>
       .
       .
       .
    </soapenv:Header>
    <soapenv:Body>
       <ns2:addOrganisationResponse ....>
       .
       .
       .
          <ns1:status>Success</ns1:status>
       </ns2:addOrganisationResponse>
    </soapenv:Body>
    </soapenv:Envelope>

This question was asked 6 months ago but maybe this answer helps somebody. 这个问题是6个月前提出的,但也许这个答案对某人有帮助。

$response = curl_exec($soap_do);
$header_size = curl_getinfo($soap_do,CURLINFO_HEADER_SIZE);
$result['header'] = substr($response, 0, $header_size);
$result['body'] = substr($response, $header_size);
$xmlStr = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $result['body']);
$resultXml = new SimpleXMLElement($xmlStr);
$xmlArray = (json_decode(json_encode($resultXml),true));
$status = $xmlArray ['soapenvBody']['ns2addOrganisationResponse']['ns1status'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM