【问题标题】:Show cURL response in XML在 XML 中显示 cURL 响应
【发布时间】:2017-09-19 03:47:01
【问题描述】:

我收到了 SOAP API 的响应,响应有标题、正文、状态等。我想从响应中获取状态。虽然响应是 xml 格式,但当我回显响应时,它显示在没有 xml 标记的一行上。 当我在下面使用时,它以 xml 格式显示:

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

现在我想从这个 xml 中获取状态。 我正在按照以下方式进行操作,但没有得到任何东西

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

还有这样的

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

如何获得状态?状态是“成功”或“失败”

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>

【问题讨论】:

    标签: php xml curl


    【解决方案1】:

    这个问题是 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'];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2010-10-08
      • 2017-07-15
      • 2014-08-25
      • 2011-09-17
      相关资源
      最近更新 更多