【问题标题】:Trouble Parsing SOAP response in PHP using simplexml使用 simplexml 在 PHP 中解析 SOAP 响应时遇到问题
【发布时间】:2012-01-03 12:14:20
【问题描述】:

我正在使用 cURL 发布 SOAP 请求。回复如下:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
  <wsa:Action>http://www.csapi.org/schema/parlayx/common/v3_1/TerminalLocationPort/getLocationForGroupResponse</wsa:Action>
</env:Header>
<env:Body>
  <ns2:getLocationForGroupResponse xmlns:ns2="http://www.csapi.org/schema/parlayx/terminal_location/v3_1/local">
     <ns2:result>
        <address>234983</address>
        <reportStatus>Retrieved</reportStatus>
        <currentLocation>
           <latitude>12.5665</latitude>
           <longitude>43.7708</longitude>
           <timestamp>2012-01-03T17:06:16.805+01:30</timestamp>
        </currentLocation>
     </ns2:result>
     <ns2:result>
        <address>423903</address>
        <reportStatus>Retrieved</reportStatus>
        <currentLocation>
           <latitude>12.2165</latitude>
           <longitude>43.6518</longitude>
           <timestamp>2012-01-03T17:06:16.824+01:30</timestamp>
        </currentLocation>
     </ns2:result>
  </ns2:getLocationForGroupResponse>
</env:Body>
</env:Envelope>

我用这个来解码:

$err = curl_error($soap_do);
$result = curl_exec($soap_do);
$xml = simplexml_load_string($result);
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['env']);
$getaddressresponse = $soap->body->children($ns['ns2']);
foreach ($getaddressresponse->children() as $item) {
    echo (string) $item->address . '<br />';
}

我在使用 SimpleXML 解码时遇到了问题。这个link 似乎与我的情况最相关,但我无法将它作为 simpleXML 元素应用到我的案例中

Warning: SimpleXMLElement::children() [simplexmlelement.children]: Node no longer exists in C:\.php on line 33 /*(line with the for each statement)*/

有什么建议吗?

更新: 如果服务器响应以下错误,我将如何检测它..?

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
      <wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
      <wsa:Action>http://www.w3.org/2005/08/addressing/fault</wsa:Action>
   </env:Header>
   <env:Body>
      <env:Fault>
         <faultcode>env:Server</faultcode>
         <faultstring>Service Exception</faultstring>
         <detail>
            <ns1:ServiceException xmlns:ns1="http://www.csapi.org/schema/parlayx/common/v3_1" xmlns:ns2="http://www.csapi.org/schema/parlayx/terminal_location/v3_1/local">
               <messageId>SVC004</messageId>
               <text>Trip not Found for this MSISDN</text>
            </ns1:ServiceException>
         </detail>
      </env:Fault>
   </env:Body>
</env:Envelope>

【问题讨论】:

    标签: php xml soap


    【解决方案1】:

    变量和属性名称区分大小写,当我测试它时,结果发现还有其他东西。以下作品:

    $soap = $xml->children($ns['env']);
    $getaddressresponse = $soap->Body->children($ns['ns2']);
    foreach ($getaddressresponse->getLocationForGroupResponse->children($ns['ns2']) as $item)
    {
        $item = $item->children();
        echo $item->address . '<br />';
    }
    

    回答更新后的问题:

    $fault = $soap->Body->children($ns['env']);
    if (isset($fault->Fault))
    {
        // Handle error
    }
    

    【讨论】:

    • 如果您也能回复更新后的问题,我们将不胜感激。谢谢
    • 如何获得纬度/经度属性?
    • 知道了! echo (string) $item->currentLocation->纬度
    • 与到达 Body、Fault 等的方式相同。获取其父节点的子节点,例如$currentLocation = $item-&gt;currentLocation-&gt;children(); echo $currentLocation-&gt;latitude;
    • 希望我能放弃多次!再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2014-06-20
    • 1970-01-01
    相关资源
    最近更新 更多