使用 simplexml 在 PHP 中解析 SOAP 响应时遇到问题

发布于 2024-12-23 16:37:10 字数 3250 浏览 0 评论 0原文

我正在使用 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 解码它时遇到问题。这个链接似乎与我的情况最相关,但我无法将其应用于我的case 作为 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>

I'm using cURL to POST a SOAP request. The response is as follows:

<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>

I use this to decode:

$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 />';
}

I'm having trouble decoding this with SimpleXML. This link seems most relevant to my situation but I'm unable to apply it to my case as the simpleXML element just

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

Any suggestions?

UPDATE:
If the server responds with the following error, how would I detect it..?

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2024-12-30 16:37:10

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

$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
}

Variable and property names are case sensitive and while I was testing it, it turned out there's other stuff as well. The following works:

$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 />';
}

To answer the updated question:

$fault = $soap->Body->children($ns['env']);
if (isset($fault->Fault))
{
    // Handle error
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文