I have the follow xml where the first node is the <Cancellation>
<?xml version='1.0' encoding='UTF-8'?>
<Cancellation>
<version>message-version</version>
<customerID>customer-identifier</customerID>
<invoiceID>invoice-number</invoiceID>
<cancellationDate>yyyy-mm-dd</cancellationDate>
<reason>reason</reason>
<reasonCode>reason-code</reasonCode>
<attempts>attempts-count</attempts>
<merchantID>rocketgate merchant-identifier</merchantID>
<merchantSiteID>site-id</merchantSiteID>
<udf01>user-data</udf01>
</Cancellation>
Eventually I could have a similar xml but for a complete different process like registration, like the follow:
<?xml version='1.0' encoding='UTF-8'?>
<Registration>
<version>message-version</version>
<customerID>customer-identifier</customerID>
<invoiceID>invoice-number</invoiceID>
<merchantID>rocketgate merchant-identifier</merchantID>
<merchantSiteID>site-id</merchantSiteID>
<udf01>user-data</udf01>
</Registration>
I will need to catch this first node in a if condition to redirect to the appropriate path.
Following this link I got an array from the sent xml:
$xmlString = trim(file_get_contents('php://input'));
$xmlObj = simplexml_load_string($xmlString);
$xmlJSON = json_encode($xmlObj);
$xmlArray = json_decode($xmlJSON, true);
But the issue is that I can't see the first <Cancellation> or <Registration> node after passing the xml string to an object.
Even the examples in the php doc is not showing the first node.
I need to test if it is a Cancellation or a Registration. How could I do that?
To find the tag name of the root element in XML just use getName() on the SimpleXMLElement...
echo $xmlObj->getName();
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.
I am having serious issues parsing this xml array using curl. I only need the click_id printed on each new row. Does someone have an example of how I can pull this. I am using curl and get the response below in the variable $result. I am using php.
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfClick xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://cakemarketing.com/api/1/">
<Click>
<click_id>7458165</click_id>
<request_session_id>7901644</request_session_id>
<click_date>2011-09-08T13:53:37.143</click_date>
<offer_id>10346</offer_id>
<advertiser_id>1050</advertiser_id>
<campaign_id>6527</campaign_id>
</Click>
<Click>
<click_id>7459318</click_id>
<request_session_id>7903011</request_session_id>
<click_date>2011-09-08T14:41:37.953</click_date>
<offer_id>10346</offer_id>
<advertiser_id>1050</advertiser_id>
<campaign_id>6527</campaign_id>
</Click>
The best way is to use XPath with the help of this class http://php.net/manual/en/class.domxpath.php
A more simple method is to use regex (but I discourage this approach)
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
This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 3 years ago.
I have this ugly XML which has alot of namespaces on it, when I try to load it with simpleXML if i indicate the first namespace I'd get an xml object ,but following tags with other namespaces would not make it to the object.
How can I parse this XML ?
<?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:Header>
<eb:MessageHeader xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" eb:version="1.0" soap-env:mustUnderstand="1">
<eb:From>
<eb:PartyId eb:type="URI">wscompany.com</eb:PartyId>
</eb:From>
<eb:To>
<eb:PartyId eb:type="URI">mysite.com</eb:PartyId>
</eb:To>
<eb:CPAId>something</eb:CPAId>
<eb:ConversationId>moredata.com</eb:ConversationId>
<eb:Service eb:type="compXML">theservice</eb:Service>
<eb:Action>theaction</eb:Action>
<eb:MessageData>
<eb:MessageId>a certain messageid</eb:MessageId>
<eb:Timestamp>2009-04-11T18:43:58</eb:Timestamp>
<eb:RefToMessageId>mid:areference</eb:RefToMessageId>
</eb:MessageData>
</eb:MessageHeader>
<wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext">
<wsse:BinarySecurityToken valueType="String" EncodingType="wsse:Base64Binary">an impresive binary security toekn</wsse:BinarySecurityToken>
</wsse:Security>
</soap-env:Header>
<soap-env:Body>
<SessionCreateRS xmlns="http://www.opentravel.org/OTA/2002/11" version="1" status="Approved">
<ConversationId>the goodbye token</ConversationId>
</SessionCreateRS>
</soap-env:Body>
</soap-env:Envelope>
im trying to parse it with the following code
<?php
$xml = simplexml_load_string($res,NULL,NULL,"http://schemas.xmlsoap.org/soap/envelope/");
?>
but the $xml object would only contain the following
SimpleXMLElement Object
(
[Header] => SimpleXMLElement Object
(
)
[Body] => SimpleXMLElement Object
(
)
)
I think you need to register the namespacing and access with XPath. Something like the following should get you going (I haven't the facility to test this).
$xml = simplexml_load_string($res, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
$xml->registerXPathNamespace('soap-env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('eb', 'http://www.ebxml.org/namespaces/messageHeader');
$xml->registerXPathNamespace('wsse', 'http://schemas.xmlsoap.org/ws/2002/12/secext');
Then you can do something like:
foreach($xml->xpath('//eb:MessageHeader') as $header)
{
var_export($header->xpath('//eb:CPAId')); // Should output 'something'.
}
You may not need to register the namespacing, thinking about it, as they are alredy present in the XML. Not sure on this though, would need to test.
Hope this helps.
1) Do not use print_r and friends to see what is "in" a SimpleXML object. See https://github.com/IMSoP/simplexml_debug for explanation and alternatives.
2) Namespace support in SimpleXML is provided by the ->children() and ->attributes() methods.
For example you could get the PartyId of the From node like this:
$from_party = (string)$xml->children('soap-env', true)->Header->children('eb', true)->MessageHeader->From->PartyId;
For anyone else that comes across this I scratched my head trying to return the correct data and although the top answer was extremely close it still took me a while to find the answer. Eventually used this page to help: https://www.w3schools.com/php/func_simplexml_registerxpathnamespace.asp
I believe the for loop can directly access what you need. i.e.
foreach($xml->xpath('//eb:CPAId') as $header)
{
echo $header; // Should output 'something'.
}
That's a soap-envelope. You might want to use a soap-client to abstract all the xml-parsing away. PHP comes with a rather good soap-client included as default.
Try this
$soap_url = 'http://path/wsdl/somefile.wsdl';
$soap_client = new SoapClient($soap_url);
var_dump($soap_client->__getFunctions());
For more detail read here