Welcome to WebmasterWorld Guest from

Forum Moderators: httpwebwitch

Message Too Old, No Replies

XML with PHP5.3 extract data - newbie



9:24 am on May 24, 2011 (gmt 0)

Hi, please forgive my ignorance but i am new to XML and its interaction with PHP5.3. I have experimented with simpleXMLElement but can only seem to extract data from the very top level of the data returned i.e. Transaction and Code. Perhaps i am up a gumtree!

All i need to get is the reply data to SMStext messages sent via a SMStext webservice. The data will then via PHP be fed back into the MYSQL database that logs outgoing and incoming SMS messages. The XML reply is in this format below. I have visited several web help pages but i am floundering. Just a simple line of code to loop through any replies would be most helpful.
Any ideas please?

Many thanks, an XML newbie!

<Description>Transaction OK</Description>
<Body>hello world</Body>
<Body>Hello world</Body>

[edited by: httpwebwitch at 1:26 pm (utc) on May 24, 2011]
[edit reason] removed phone numbers from the example [/edit]


1:39 pm on May 24, 2011 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

once you've created your SimpleXML object, use print_r() to see its structure.

$obj = simplexml_load_string($xml);

that method works well in cases like yours where you're not accessing data inside XML attributes and element values. When your data has mixed attributes and values, print_r() is ineffective because it converts your objects into arrays first. There's an explanation here:
[php.net...] and more good examples here: [php.net...]

Once you view the structure using print_r(), it should be easy to see how you get to the innards.

Something like this might work:

foreach ($obj->GetSMSReplyResponse->Replies->Reply as $reply) {
echo $reply->Body;


5:50 pm on May 24, 2011 (gmt 0)

Many thanks, I'll look at this later.PH.

Featured Threads

Hot Threads This Week

Hot Threads This Month