0

I am trying to get information about an IP address from api.hostip.info but the following code returns the current XML version;

$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$url = 'http://api.hostip.info/?ip=12.215.42.19';

$xml = file_get_contents($url, false, $context);
$xml = simplexml_load_string($xml);
var_dump($xml);

How can I return the data from the URL?

hakre
  • 193,403
  • 52
  • 435
  • 836
Airikr
  • 6,258
  • 15
  • 59
  • 110
  • Na. I want to get the information from api.hostip.info so I can match `countryAbbrev` with my function I have. `$xml = htmlentities($xml);` did by the way return many new lines with no text – Airikr Apr 16 '13 at 13:17

1 Answers1

0

Because the XML contains colons (namespace), you have to do it a bit differently and use this method to access the namespace:

var_dump($xml->children('gml', true)->featureMember->children()->Hostip);

/*
    object(SimpleXMLElement)#3 (5) {
      ["ip"]=>
      string(12) "12.215.42.19"
      ["countryName"]=>
      string(13) "UNITED STATES"
      ["countryAbbrev"]=>
      string(2) "US"
      ["comment"]=>
      object(SimpleXMLElement)#2 (0) {
      }
      ["ipLocation"]=>
      object(SimpleXMLElement)#5 (0) {
      }
    }
*/

..should give you exactly what you want:

Community
  • 1
  • 1
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102