Parsing XML data from hostip.info 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 hostip.info 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 hostip.info call such as http://api.hostip.info/?ip=[IP-ADDRESS]

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <HostipLookupResultSet version="1.0.1" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">
  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>12.215.42.19</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="http://www.opengis.net/gml/srs/epsg.xml#4326">
  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('http://api.hostip.info/?ip='.$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 = '12.215.42.19';
  2. $location_info = get_location($ip);
  3. var_dump($location_info);

The dump would look something like this:

array(4) {
  ["longitude"]=>
  string(8) "-74.9072"
  ["latitude"]=>
  string(7) "39.9499"
  ["citystate"]=>
  object(SimpleXMLElement)#5 (1) {
    [0]=>
    string(16) "Mount Laurel, NJ"
  }
  ["country"]=>
  object(SimpleXMLElement)#2 (1) {
    [0]=>
    string(13) "UNITED STATES"
  }
}

To access the longitude:
$location_info['longitude'];

To access the latitude:
$location_info['latitude'];

To access the city/state string:
$location_info['citystate'];

To access the country string:
$location_info['country'];

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 hostip.info 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!
    JD-Miami

Leave a Reply

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