Pharse XML in PHP

54 views
Skip to first unread message

Ryan524

unread,
Sep 30, 2010, 7:50:44 PM9/30/10
to The Easy API
I am making api calls from a php script to the Zip Code Radius Search.
The XML response though contains alot of data that I don't really
need, what I need is a list or array of the actual zipcodes that fall
within the specified radius. WHat is the easiest way to pharse the
XML to obtain the each of the <return><zipcodes><zipcode> items
retuened.

C. Smith

unread,
Oct 1, 2010, 8:02:46 AM10/1/10
to The Easy API
Hey Ryan,

That's a really good question. What I typically use is SimpleXML it's
a PHP5 only extension and comes pre-installed with any PHP5
installation. It's extremely easy to use, you would get the return
from the server and do something like this:

<?php
// Obviously this below should be the return from the API
$get_return = "<return><zipcodes><zipcode>12345</
zipcode><zipcode>67808</zipcode></zipcodes></return>";
$simple_xml = simplexml_load_string($get_return);

// Dump to see what we are getting back and the structure of simplexml
echo "<pre>";
print_r($simple_xml);
echo "</pre>";

foreach($simple_xml->zipcodes->zipcode as $tmp_zip){
echo $tmp_zip ."<br />";
}
?>

One nice thing with SimpleXML is that it can be typcasted. That's to
say you can typecast a return into string, int, or array easily. I can
tell you that the backend of The Easy API uses that feature a lot.

Take care and let me know if you need any other help,
Chad

Ryan524

unread,
Oct 1, 2010, 2:20:19 PM10/1/10
to The Easy API
Thank you that is very helpful, though for some reason its obly
listing 1 zipcode but the retuened xml has several zipcodes. You can
see my full code at http://pastebin.com/qzipFTcJ for my simple test
script. When the full api response is outputted I see several
zipcodes returned but in the for each loop where it prints each
individual zipcode I see only the first zipcode returned.
> > retuened.- Hide quoted text -
>
> - Show quoted text -

C. Smith

unread,
Oct 1, 2010, 2:30:47 PM10/1/10
to The Easy API
Hey Ryan,

Check out this pastebin: http://pastebin.com/rjLzWgn5

You will see that I am going to typecast the returned simplexml object
(because it's not heavy on resources) and then it echo's out:
zipcode-city-state

Check out the array for $tmp_zip to see what information is available
for your application.

If you need anything else let me know,
Chad

On Oct 1, 2:20 pm, Ryan524 <rbile...@gmail.com> wrote:
> Thank you that is very helpful, though for some reason its obly
> listing 1 zipcode but the retuened xml has several zipcodes.  You can
> see my full code athttp://pastebin.com/qzipFTcJfor my simple test

Ryan524

unread,
Oct 1, 2010, 2:50:53 PM10/1/10
to The Easy API
Thanks for that, that does the trick. One thing though, I don't know
if its the code of the api, but I have some zipcodes being returned
twice. I don't know if thats the api doing that or my code is making
duplicates, but it occurs both in the list made by the foreach loop
and the "print_r($simple_xml);" Now I can work around this, by
loading each zipcode into my own array and checking before I load
eachone to see if it already exists in the array but I though you
should know in case it is a problem on your end with the api, or maybe
there is a reason it does that I am not aware of.

Thanks again for your help though.

On Oct 1, 11:30 am, "C. Smith" <chadsmith...@gmail.com> wrote:
> Hey Ryan,
>
> Check out this pastebin:http://pastebin.com/rjLzWgn5
>
> You will see that I am going to typecast the returned simplexml object
> (because it's not heavy on resources) and then it echo's out:
> zipcode-city-state
>
> Check out the array for $tmp_zip to see what information is available
> for your application.
>
> If you need anything else let me know,
> Chad
>
> On Oct 1, 2:20 pm, Ryan524 <rbile...@gmail.com> wrote:
>
>
>
> > Thank you that is very helpful, though for some reason its obly
> > listing 1 zipcode but the retuened xml has several zipcodes.  You can
> > see my full code athttp://pastebin.com/qzipFTcJformy simple test
> > > - Show quoted text -- Hide quoted text -
Reply all
Reply to author
Forward
0 new messages