0

I use following method to get webservice data,

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


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

When I var_dump $result I get,

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)

How I can get the value from child node <CustomerID> and store into an array??

Thank you..

1
  • This is obviously not the whole output of the var_dump. As long as the WSDL properly covers it, you should get some object you simply can access a property on. Otherwise use an XML parser (e.g. SimpleXML) parse the XML of the any property. Commented Mar 6, 2013 at 4:54

2 Answers 2

3

Normally the SoapClient should provide a stdClass object that offers these values as standard class properties if you're in WSDL mode.

If that is not the case (it's not clear from the var_dump in your question because it is cut-off) you can parse the XML you have in the any property, for example by using the SimpleXML parser.

Some code example (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";

It shows two alternative ways to access the data, the first with standard traversal and the second via the xpath() method running an xpath query.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, and let me try :)
0

You can always use regexp:

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

7 Comments

Yh its works..thanks..but when I try to get <MainLoc> its fails :(
That's because you need different regexp for MainLoc content, it's not a number, try replacing '#<CustomerID>(\d+)</CustomerID>#' with '#<MainLoc>(\w+)</MainLoc>#'
yes! it works..Can you please help me getting the whole xml content from $result and storing it as a string?
Use preg_match_all('#<[^>]*>([^<]*)</[^>]*>#', $result->GetAlladdressResult->any, $matches); and in $matches you will find all of the content of the xml you've received
that regex is not fitting to parse valid XML. it's actually quite far off and does not offer any control like, taking the third of such child element inside that parent element and so on and so forth.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.