从PHP中的Soap响应stdclass对象获取数据

时间:2013-03-05 18:30:39

标签: php xml web-services soap

我使用以下方法获取webservice数据,

$client = new SoapClient("http://empblr.dyndns.org/CentralHomeDelivery_Mob/Service.asmx?wsdl");


     $result = $client->GetAlladdress(array('customerid'=>36));

当我得到var_dump $ result时,

object(stdClass)[16]
  public 'GetAlladdressResult' => 
    object(stdClass)[17]
      public 'schema' => string '<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Table"><xs:complexType><xs:sequence><xs:element name="AdressID" type="xs:int" minOccurs="0"/><xs:element name="CustomerID" type="xs:int" minOccurs="0"/><xs:element name="LocationID" type="xs:int" minOccurs="0"/><xs'... (length=1030)
      public 'any' => string '<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><NewDataSet xmlns=""><Table diffgr:id="Table1" msdata:rowOrder="0"><AdressID>643</AdressID><CustomerID>36</CustomerID><LocationID>176</LocationID><StreetName>asdf</StreetName><HouseNo>1234</HouseNo><AlternatePhone>5632256</AlternatePhone><LandMark>asdf </LandMark><MainLocID>2</MainLocID><locName>ANDOLANA CIRCLE</locName><MainLoc>Mysore    </MainLoc></Table><Table diffgr:id="Table2" m'... (length=1864)

如何从子节点<CustomerID>获取值并存储到数组中?

谢谢..

2 个答案:

答案 0 :(得分:3)

通常SoapClient应提供stdClass对象,如果您处于WSDL模式,则将这些值作为标准类属性提供。

如果不是这种情况(问题中的var_dump并不清楚,因为它已被截止),您可以解析any属性中的XML,例如使用SimpleXML parser

一些代码示例(Online Demo):

$xml = new SimpleXMLElement($result->GetAlladdressResult->any);

# traversal
$table = $xml->NewDataSet->Table[0];
echo $table->LocationID, "\n", $table->MainLoc, "\n";

# xpath
echo $xml->xpath('//LocationID')[0], "\n", $xml->xpath('//MainLoc')[0], "\n";

它显示了两种访问数据的替代方法,第一种是使用标准遍历,第二种是通过运行xpath查询的xpath()方法。

答案 1 :(得分:0)

您始终可以使用regexp:

preg_match('#<CustomerID>(\d+)</CustomerID>#', $result->GetAlladdressResult->any, $match);
$customerId = isset($match[1]) ? $match[1] : false;