andy's php projects
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Script and site usage:
All of the scripts on this site are provided free for non-commerical, personal or non-profit use. If you find these scripts useful to you, appreciate the free support, or are just an all round nice person, then why not donate a little dosh? If you wish to use any of these in a commercial system, please refer to the payment plan.


Parsing XML back into a page.

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> PHP Questions
View previous topic :: View next topic  
Author Message
MrLimo
Newbie


Joined: 29 Jan 2005
Posts: 3
Location: Albany, OR

PostPosted: Tue Apr 10, 2007 1:06 pm    Post subject: Parsing XML back into a page. Reply with quote

I'm trying to locate some hints on how to parse xml returned from an REST API call back into usefull information.

Ie.
API Call {Edited} npanxx (areacode exchange or full 10 digit us number)
http://www.termination.com/api//?key=8ade.....e07&username=bogus-username&method=term.getRate&number=541926
Returns the following xml

<?xml version="1.0" encoding="UTF-8" ?>
- <rsp stat="ok" version="1.0">
<rate npa="541" nxx="926" initial_duration="6" bill_duration="6" intra_state="0.0173" inter_state="0.0152" />
</rsp>


I would like to create a page that has two boxes one for the npa and one for the nxx and a lookup rates.

Enter an NPA/NXX to lookup the rate 541-926 & Lookup Rate click box.

After processing should return something like:
NPA/NXX Rate center Rate
541-926 ALBANY $0.0156

And allow you to enter another npa-nxx to lookup.

Greg Smile
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Tue Apr 10, 2007 3:56 pm    Post subject: Reply with quote

Are you using PHP4 or PHP5?
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
MrLimo
Newbie


Joined: 29 Jan 2005
Posts: 3
Location: Albany, OR

PostPosted: Tue Apr 10, 2007 4:20 pm    Post subject: Reply with quote

PHP Version 4.3.11
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Fri Apr 13, 2007 11:18 am    Post subject: Reply with quote

Well, there are a couple ways you could do this. The proper way would be to use a 3rd party xml library to parse the result you get in to an array. Of course, you could use the built-in xml extension to write your own parser, but sometimes it's just easier to use something that's already out there. One I quite like is MiniXML - http://minixml.psychogenic.com/ - or there's the PEAR XML_Parser. Just do a search on your favourite search engine and you'll find a bunch of others! Once you've got it parsed in to an array or object it should be easy to pull out the info. One reason I quite like MiniXML is because it gives XPath type lookups.

The other way you could do it, is just to parse the XML with something like preg_match. It's a fairly safe bet that, if the web service call is successful, the structure will always look something like:

Code:
<rate npa="541" nxx="926" initial_duration="6" bill_duration="6" intra_state="0.0173" inter_state="0.0152" />


So you could do something like:

Code:
$found = preg_match('!<rate npa="(\d+)" nxx="(\d+)" initial_duration="(\d+)" bill_duration="(\d+)" intra_state="(.*?)" inter_state="(.*?)" />!', $xml, $matches);
if ($found) {
    print_r($matches);
} else {
    // something may have gone wrong
}


Regards,

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> PHP Questions All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group