Parsing XML data from API service with PHP

If you ended up here, then you most likely experienced what I did in trying to figure out how to parse the XML data from the web API service.

Basically, it is down to the namespaces that are not accessible by normal means.

Here is the basic XML response from a typical call such as[IP-ADDRESS]

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <HostipLookupResultSet version="1.0.1" xmlns:gml="" xmlns:xsi="" xsi:noNamespaceSchemaLocation="">
  3.    <gml:description>This is the Hostip Lookup Service</gml:description>
  4.    <gml:name>hostip</gml:name>
  5.    <gml:boundedBy>
  6.       <gml:Null>inapplicable</gml:Null>
  7.    </gml:boundedBy>
  8.    <gml:featureMember>
  9.       <Hostip>
  10.          <ip></ip>
  11.          <gml:name>Sugar Grove, IL</gml:name>
  12.          <countryName>UNITED STATES</countryName>
  13.          <countryAbbrev>US</countryAbbrev>
  14.          <!-- Co-ordinates are available as lng,lat -->
  15.          <ipLocation>
  16.             <gml:pointProperty>
  17.                <gml:Point srsName="">
  18.                   <gml:coordinates>-88.4588,41.7696</gml:coordinates>
  19.                </gml:Point>
  20.             </gml:pointProperty>
  21.          </ipLocation>
  22.       </Hostip>
  23.    </gml:featureMember>
  24. </HostipLookupResultSet>

Here is a nice PHP function that will return all the important pieces for you:

  1. function get_location($ip) {
  2. 	$content = @file_get_contents(''.$ip);
  3. 	if ($content != FALSE) {
  4. 		$xml = new SimpleXmlElement($content);
  5. 		$coordinates = $xml->children('gml', TRUE)->featureMember->children('', TRUE)->Hostip->ipLocation->children('gml', TRUE)->pointProperty->Point->coordinates;
  6. 		$longlat = explode(',', $coordinates);
  7. 		$location['longitude'] = $longlat[0];
  8. 		$location['latitude'] = $longlat[1];		
  9.                 $location['citystate'] = '==>'.$xml->children('gml', TRUE)->featureMember->children('', TRUE)->Hostip->children('gml', TRUE)->name;
  10. 		$location['country'] =  '==>'.$xml->children('gml', TRUE)->featureMember->children('', TRUE)->Hostip->countryName;
  11. 		return $location;
  12. 	}
  13. 	else return false;
  14. }

Here is the usage:

  1. $ip = '';
  2. $location_info = get_location($ip);
  3. var_dump($location_info);

The dump would look something like this:

array(4) {
  string(8) "-74.9072"
  string(7) "39.9499"
  object(SimpleXMLElement)#5 (1) {
    string(16) "Mount Laurel, NJ"
  object(SimpleXMLElement)#2 (1) {
    string(13) "UNITED STATES"

To access the longitude:

To access the latitude:

To access the city/state string:

To access the country string:

I did not bother to parse the city/state string because I’m not sure if this format is consistent for international addresses.

Anyway, I hope this code helps someone out there =)

Published by

Allen Liu

Building websites is a passion of mine. During the course of my work, I have created some snippets of code that I hope will prove useful to other developers.

7 thoughts on “Parsing XML data from API service with PHP”

  1. spent a couple hours learning how to parse gml for the first time and this was all i needed ! Thanks for taking the time to put it up!

Leave a Reply

Your email address will not be published. Required fields are marked *