Accessing XML content via PHP using SimpleXML - php

I am working with the XML file string below and I've tried a number of methods to try and get access to certain parts of the XML contents. Please see the code after the XML file below for my attempt:
<?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>
<Address_ListResponse xmlns="http://example.example.com/">
<Address_ListResult>
<Address>
<HoldingId xsi:nil="true"/>
<MainId>1617931</MainId>
<ContactId>8</ContactId>
<Description>Home, All Purposes</Description>
<Position/>
<Department/>
<Organisation/>
<AddressLabel>Mr Joe Bloggs</AddressLabel>
<AddressLine1>1 Fake Road</AddressLine1>
<AddressLine2/>
<AddressLine3/>
<Town>Faketown</Town>
<CountyId>818</CountyId>
<PostCode>FA33 4KE</PostCode>
<CountryId>3</CountryId>
<Phone>01234567890</Phone>
<EvePhone/>
<Mobile/>
<Email>joe#bloggs.com</Email>
<Fax/>
<WWW/>
<AddressTypeId>1</AddressTypeId>
<IsBilling>true</IsBilling>
<IsMailing>true</IsMailing>
<IsDelivery>true</IsDelivery>
<IsInherited>false</IsInherited>
<GridN/>
<GridE/>
<Latitude/>
<Longitude/>
<CensationCode/>
<IsDeleted>false</IsDeleted>
<HoldingPersonalDetailsId xsi:nil="true"/>
<IsSynced>false</IsSynced>
<BeenProcessed>false</BeenProcessed>
<CountyName/>
<CountryName/>
<AddressTypeName>Home</AddressTypeName>
</Address>
</Address_ListResult>
</Address_ListResponse>
</soap:Body>
</soap:Envelope>
Code for accessing the XML content:
$xml = simplexml_load_string($result);
echo "Town: " . $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->children('http://example.example.com/')->Address_ListResponse->Town;
The above code was based on a link posted by another StackOverFlow question: http://blog.preinheimer.com/index.php?/archives/172-SimpleXML,-Namespaces-Hair-loss.html
Any help would be appreciated.
Thanks.

Consider using the SOAP extension instead.
See the example in the PHP Manual on how to write a client.
An alternative would be to use Zend_Soap as a standalone component.

Turns out the answer I was looking for wasn't SimpleXML - or at least I couldn't get that to work.
What I have done is used the xml_parse_into_struct to create an array of values returned from the XML data: http://www.php.net/manual/en/function.xml-parse-into-struct.php

Related

Pass raw xml to SOAP in PHP

If I have a raw XML message that I need to pass in PHP, is there an easy way to pass it?
Something like this:
$xml = '
<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
...
</soap:Header>
<soap:Body>
...
<soap:Fault>
...
</soap:Fault>
</soap:Body>
</soap:Envelope>';
$url = 'http://www.myurl.com';
passXmlToSoap($xml,$url);
I am not trying to master using SOAP and I only need to use it to do a very simple thing so I am hoping that I can use raw XML and do it as simply as possible even though that might not be "the right way" to do it.
check this answer:
How to parse SOAP XML?
there should be libraries for parsing XML, but that answer gives you an straightforward way to do it. Good Luck
it works:
$xml = '<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Header>
<Name>Yo</Name>
</soap:Header>
<soap:Body>
<payment>
<uniqueReference>ESDEUR11039872</uniqueReference>
<epacsReference>74348dc0-cbf0-df11-b725-001ec9e61285</epacsReference>
<postingDate>2010-11-15T15:19:45</postingDate>
<bankCurrency>EUR</bankCurrency>
<bankAmount>1.00</bankAmount>
<appliedCurrency>EUR</appliedCurrency>
<appliedAmount>1.00</appliedAmount>
<countryCode>ES</countryCode>
<bankInformation>Sean Wood</bankInformation>
<merchantReference>ESDEUR11039872</merchantReference>
</payment>
</soap:Body>
</soap:Envelope>';
$xml = simplexml_load_string($xml);
print_r( $xml) ;
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
foreach ($xml->xpath('//payment') as $item)
{
print_r($item);
}
In any case you should never have to parse XML response nor pass XML request to consume SOAP Web Service.
First if you use the native php SoapClient class you send an object or an array. Then you receive stdClass objects
Second, you should use a WSDL to php generator that generates classes mathing the parameters that has to be sent. It also generated the classes that match the response object. Finally, it generated the classes to send the request. So you can send the request and receive the response very easily without any doubt and without having to deal with XML.
Try PackageGenerator project.

Trouble Accessing XML after using simplexml

Before someone points out that there are a ton of similar questions like this, please keep in mind I have tried and exhausted all methods I could find here on stacked.
I'm having trouble using simplexml to pull out the data I want from a response that is structured like this.
<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>
<authenticateresponse xmlns="http://somesite.co.nz">
<authenticateresult>
<username>Username</username>
<token>XXXXXXXXX</token>
<reference>
<message>Access Denied</message>
</reference>
</authenticateresult>
</authenticateresponse>
</soap:body>
In this case I'd like to know how to pull out the token and username.
Your XML has default namespace declared at authenticateresponse element :
xmlns="http://somesite.co.nz"
Notice that the element where default namespace is declared along with the descendant elements without prefix are in the same namespace. To access element in default namespace, you need to map a prefix to the namespace URI and use the prefix in the XPath, for example :
$raw = <<<XML
<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>
<authenticateresponse xmlns="http://somesite.co.nz">
<authenticateresult>
<username>Username</username>
<token>XXXXXXXXX</token>
<reference>
<message>Access Denied</message>
</reference>
</authenticateresult>
</authenticateresponse>
</soap:body>
</soap:envelope>
XML;
$xml = new SimpleXMLElement($raw);
$xml->registerXPathNamespace('d', 'http://somesite.co.nz');
$username = $xml->xpath('//d:username');
echo $username[0];
eval.in demo
output :
Username
A few former related questions :
Parsing XML with PHP's simpleXML
XPath in SimpleXML for default namespaces without needing prefixes

Can't parse XML using simplexml_load_string in PHP

How to pass the following message in PHP?
<?xml version="1.0" encoding="utf-8"?>
<response>
<action>sendmessage</action>
<data>
<acceptreport>
<statuscode>0</statuscode>
<statusmessage>Message accepted for delivery</statusmessage>
<messageid>8abbaf6c-3bdd-4fb7-9c49-282270bbb309</messageid>
<originator>admin</originator>
<recipient>233xxx</recipient>
<messagetype>SMS:TEXT</messagetype>
<messagedata>mdata</messagedata>
</acceptreport>
</data>
</response>
I have tried
$xml = simplexml_load_string($data);
var_dump($xml['data']);
and
var_dump($xml->attributes());
Nothing seems to be working. Any clue? What am I missing?
Try
var_dump($xml->data->acceptreport->messagedata);
$xml->attributes() would be trying to get any attributes on the root node (<response>), and that node has no attributes.

Accessing a soap xml node in PHP

After breaking my head on a wall for a whole day i just think i need some help for this.
I'm receiving the following answer from a soap call :
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<SearchBrochuresResponse xmlns="http://services.iceportal.com/service">
<SearchBrochuresResult>
<pageNumber>0</pageNumber>
<brochures>
<SearchBrochure>
<iceID>10427</iceID>
<city>Acapulco</city>
</SearchBrochure>
</brochures>
</SearchBrochuresResult>
</SearchBrochuresResponse>
</soap:Body>
</soap:Envelope>
I tried all i could find on the subject on stackoverflow and all the other sources i found, but i couldn't access to the iceID, basically my goal is to get into a php variable the value of the iceID node.
Thanks a lot for your help.
Assume your xml data is in the variable $data you could create a simple XML object from it, then access it's nodes in the following way (example to get iceID):
$xml = simplexml_load_string($data);
$iceID = (string)$xml->children('soap', true)
->Body->children()
->SearchBrochuresResponse
->SearchBrochuresResult
->brochures
->SearchBrochure
->iceID;
You could look into XPath, the technique used to navigate and select parts of an XML document.
Wikipedia on XPath
If you need more help please post a more specific question. What have you tried? Do you only need to extract that specific value?

getting pure xml detail from soap xml response

i have a .net service i am consuming from php and the result comes in the format below.
i wish to have only the contents without the soap:envelope.
How do i achieve this?
<?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><VAccResponse xmlns="http://apps.hbng.com/"><VAccResult><VAccountSummary><Id>C0000005</Id><AccountNo>5100000014</AccountNo><AccountName>xyz HOSPITAL LTD</AccountName><SchemeCode>abc</SchemeCode><SchemeDescription>abc records</SchemeDescription><Balance>6627282</Balance><CurrencyCode>DOLLARS</CurrencyCode><AccountManagerId>F05</AccountManagerId><Debit>0</Debit><Credit>0</Credit><Tran>NO TRANSACTION DONE</Tran></VAccountSummary></VAccResult></VAccResponse></soap:Body></soap:Envelope>
How do i get the xml result without the soap:body, soap envelope etc...

Categories