2012-01-03 158 views
1

我正在使用cURL來發布SOAP請求。響應如下:使用simplexml解析PHP中的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> 

回答

4

變量和屬性名稱區分大小寫,當我測試它時,事實證明還有其他的東西。以下作品:

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

這一工程!非常感謝! – kouton 2012-01-03 14:36:54

+0

將不勝感激,如果你可以迴應更新的問題以及..謝謝 – kouton 2012-01-03 14:42:36

+0

我將如何達到緯度/經度屬性? – kouton 2012-01-03 15:11:06