how to retrieve the values using php variable/array (nusoap)

别来无恙 提交于 2019-12-13 21:17:14

问题


Scenario: I'm using a nusoap.php and connect to a microsoft web service. It returns a result in an array. And it works.

Problem: How to retrieve the value of the DeviceId that is return from the nusoap which is an array? (Because I need to loop this array row by row and get the value of the DeviceId.).

Here's my code: (I print the whole value of an array and the xml)

<?php

require_once('nusoap.php');

$client = new nusoap_client('http://xxx.xxx.xx.xxx:8060/iapps/Service1.asmx?WSDL', true);

$param = array('sql' =>'exec DeviceGet 1 ','p2' => '');
$result = $client->call('GetData',$param , 'Service');

echo print_r($result,true);

echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
?>  

Here's the result:

Array ( [GetDataResult] => Array ( [schema] => Array ( [element] => Array ( [complexType] => Array ( [choice] => Array ( [element] => Array ( [complexType] => Array ( [sequence] => Array ( [element] => Array ( [!name] => DeviceId [!minOccurs] => 0 ) ) ) [!name] => Table1 ) [!minOccurs] => 0 [!maxOccurs] => unbounded ) ) [!name] => NewDataSet [!msdata:IsDataSet] => true [!msdata:UseCurrentLocale] => true ) [!id] => NewDataSet ) [diffgram] => Array ( [NewDataSet] => Array ( [Table1] => Array ( [0] => Array ( [DeviceId] => 00000000-0000-1000-8000-000C29BB0EEA [!diffgr:id] => Table11 [!msdata:rowOrder] => 0 ) [1] => Array ( [DeviceId] => 2f73ba3b80df3636c14e1bafa3818f06d42ebe4d [!diffgr:id] => Table12 [!msdata:rowOrder] => 1 ) [2] => Array ( [DeviceId] => 448252633b4dde13c4cf0431d4055d69ffc70723 [!diffgr:id] => Table13 [!msdata:rowOrder] => 2 ) [3] => Array ( [DeviceId] => 731a2f59b54d38df202315cd3853e3272757febd [!diffgr:id] => Table14 [!msdata:rowOrder] => 3 ) [4] => Array ( [DeviceId] => f855ded29053b276cd99452a454477e5a2d27787 [!diffgr:id] => Table15 [!msdata:rowOrder] => 4 ) ) ) ) ) )

Response
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Thu, 07 Apr 2011 21:18:12 GMT
Connection: close
Content-Length: 1598

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetDataResponse xmlns="http://tempuri.org/"><GetDataResult><xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Table1"><xs:complexType><xs:sequence><xs:element name="DeviceId" type="xs:string" minOccurs="0" /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><NewDataSet xmlns=""><Table1 diffgr:id="Table11" msdata:rowOrder="0"><DeviceId>00000000-0000-1000-8000-000C29BB0EEA</DeviceId></Table1><Table1 diffgr:id="Table12" msdata:rowOrder="1"><DeviceId>2f73ba3b80df3636c14e1bafa3818f06d42ebe4d</DeviceId></Table1><Table1 diffgr:id="Table13" msdata:rowOrder="2"><DeviceId>448252633b4dde13c4cf0431d4055d69ffc70723</DeviceId></Table1><Table1 diffgr:id="Table14" msdata:rowOrder="3"><DeviceId>731a2f59b54d38df202315cd3853e3272757febd</DeviceId></Table1><Table1 diffgr:id="Table15" msdata:rowOrder="4"><DeviceId>f855ded29053b276cd99452a454477e5a2d27787</DeviceId></Table1></NewDataSet></diffgr:diffgram></GetDataResult></GetDataResponse></soap:Body></soap:Envelope>

回答1:


foreach ($result['GetDataResult']['diffgram']['NewDataSet']['Table1'] as $item) {
    echo $item['DeviceId'] . PHP_EOL;
}

Output:

00000000-0000-1000-8000-000C29BB0EEA
2f73ba3b80df3636c14e1bafa3818f06d42ebe4d
448252633b4dde13c4cf0431d4055d69ffc70723
731a2f59b54d38df202315cd3853e3272757febd
f855ded29053b276cd99452a454477e5a2d27787


来源:https://stackoverflow.com/questions/5587911/how-to-retrieve-the-values-using-php-variable-array-nusoap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!