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>

Continue reading Parsing XML data from hostip.info API service with PHP