PHP中的“节点不再存在”错误

时间:2012-02-25 09:53:48

标签: php geolocation

我正在使用以下代码使用hostip网络服务将用户的IP转换为纬度/经度信息:

//get user's location
$ip=$_SERVER['REMOTE_ADDR'];

function get_location($ip) {
    $content = file_get_contents('http://api.hostip.info/?ip='.$ip);
    if ($content != FALSE) {
        $xml = new SimpleXmlElement($content);
        $coordinates = $xml->children('gml', TRUE)->featureMember->children('', TRUE)->Hostip->ipLocation->children('gml', TRUE)->pointProperty->Point->coordinates;
        $longlat = explode(',', $coordinates);
        $location['longitude'] = $longlat[0];
        $location['latitude'] = $longlat[1];        
                $location['citystate'] = '==>'.$xml->children('gml', TRUE)->featureMember->children('', TRUE)->Hostip->children('gml', TRUE)->name;
        $location['country'] =  '==>'.$xml->children('gml', TRUE)->featureMember->children('', TRUE)->Hostip->countryName;
        return $location;
    }
    else return false;
}

$data = get_location($ip);

$center_long=$data['latitude'];
$center_lat=$data['longitude'];

这对我来说很好,使用$ center_long和$ center_lat页面上的谷歌地图以我的城市为中心,但我在泰国有一位朋友从那里测试过,他得到了这个错误:

Warning: get_location() [function.get-location]: Node no longer exists in /home/bedbugs/registry/index.php on line 21

所以我对此完全感到困惑,如果不这样做,他怎么会得到错误?我尝试使用谷歌搜索它,它与解析XML数据有关,但解析过程对我和他来说都是一样的。请注意,第21行是以'$coordinates ='开头的。

1 个答案:

答案 0 :(得分:2)

您需要检查服务实际上是否列出了<ipLocation>,您正在执行:

$xml->children('gml', TRUE)->featureMember->children('', TRUE)->Hostip->ipLocation
    ->children('gml', TRUE)->pointProperty->Point->coordinates

但我的IP的XML输出是:

<HostipLookupResultSet version="1.0.1" xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">
  <gml:description>This is the Hostip Lookup Service</gml:description>
  <gml:name>hostip</gml:name>
  <gml:boundedBy>
    <gml:Null>inapplicable</gml:Null>
  </gml:boundedBy>
  <gml:featureMember>
    <Hostip>
      <ip>...</ip>
      <gml:name>(Unknown City?)</gml:name>
      <countryName>(Unknown Country?)</countryName>
      <countryAbbrev>XX</countryAbbrev>
      <!-- Co-ordinates are unavailable -->
    </Hostip>
  </gml:featureMember>
</HostipLookupResultSet>

最后一部分->children('gml', TRUE)->pointProperty->Point->coordinates给出了错误,因为它没有子节点(对于某些IP)。


您可以添加基本检查以查看<ipLocation>节点是否存在(假设服务始终至少返回<hostIp>节点):

function get_location($ip) {
    $content = file_get_contents('http://api.hostip.info/?ip='.$ip);
    if ($content === FALSE) return false;

    $location = array('latitude' => 'unknown', 'longitude' => 'unknown');
    $xml = new SimpleXmlElement($content);
    $hostIpNode = $xml->children('gml', TRUE)->featureMember->children('', TRUE)->Hostip;
    if ($hostIpNode->ipLocation) {
        $coordinates = $hostIpNode->ipLocation->children('gml', TRUE)->pointProperty->Point->coordinates;
        $longlat = explode(',', $coordinates);
        $location['longitude'] = $longlat[0];
        $location['latitude'] = $longlat[1];     
    }

    $location['citystate'] = '==>'.$hostIpNode->children('gml', TRUE)->name;
    $location['country'] =  '==>'.$hostIpNode->countryName;
    return $location;
}