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

时间:2014-09-29 08:38:14

标签: php xml parsing soap

我收到了来自webservice的回复:

<?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响应,并得到了正确的树。

2 个答案:

答案 0 :(得分:0)

你可以通过循环来做到这一点,因为你得到数组作为回报

foreach ($response as $res)
    {

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

    }

答案 1 :(得分:0)

我建议使用Zend Soap Client for 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