SOAP响应 - 在php中解析xml,如何访问?

问题描述:

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

您可以通过循环,你的回报

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

我recommandate使用Zend的SOAP客户端为PHP越来越阵列做到这一点。有ü可以这样做:

$client = new Zend_Soap_Client("MyService.wsdl"); 
$result = $client->yourMethod(<YouParameters ...>); 
echo $result->H1->BOGUS[1]->time; 

参见: http://framework.zend.com/manual/1.12/de/zend.soap.client.html