从api.hostip.info获取数据[重复]

This question already has an answer here:

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?

</div>

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: