Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

XML with PHP5.3 extract data - newbie

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

New User

5+ Year Member

joined:May 23, 2011
posts: 3
votes: 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)

Senior Member from CA 

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

joined:Aug 29, 2003
votes: 0

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)

New User

5+ Year Member

joined:May 23, 2011
posts: 3
votes: 0

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