2014-09-29 40 views
0

我從web服務響應:SOAP響應 - 在php中解析xml,如何訪問?

<?xml version="1.0" encoding="UTF-8"?> 
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://tempuri.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <SOAP-ENV:Body> 
       <ns1:H1 xsi:type="ns1:H1"> 
        <BOGUS> 
          <time>1411967345</time> 
          <status>1</status> 
          <speed>0</speed> 
        </BOGUS> 
        <BOGUS> 
          <time>1411964888</time> 
          <status>10</status> 
          <speed>0</speed> 
        </BOGUS> 
       </ns1:H1> 
     </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 

我如何可以訪問元素時間狀態BOGUS[0]BOGUS[1]
我嘗試這樣做:

$soap  = simplexml_load_string($str); 
    $response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://tempuri.org/')->H1; 
    $time = $response->BOGUS[1]->time; 
    echo $time; 

,但它不工作。返回:注意:試圖獲取非對象的屬性

tempuri.org是正確的。我在xmlgrid.net上粘貼了xml響應並獲得了正確的樹。

+0

** HTTP://tempuri.org/**是正確的?此外,你應該解釋你的意思,但它不是working_。 – CSchulz 2014-09-29 08:41:09

+0

是的,tempuri.org是正確的。代碼:$ time = $ response-> BOGUS [1] - > time;返回:注意:試圖獲取非對象的屬性 – scyche 2014-09-29 09:27:40

+0

您應該添加有關您的問題的信息,作爲編輯您的問題而不是評論。 :) – CSchulz 2014-09-29 09:36:59

回答

0

您可以通過循環,你的回報

foreach ($response as $res) 
    { 

    $time = $res->BOGUS[1]->time; 
    echo $time; 

    } 
+0

$ time = $ response-> BOGUS [1] - > time; ??這是對的嗎?所以變量$ res是未使用的。 – scyche 2014-09-29 09:33:43

+0

它應該是$ res對不起,我更新了 – 2014-10-01 11:17:15