-2

我想使用 php 从 IP 地址中查找国家、城市、纬度和经度。我正在使用这个 url,它以 xml 格式返回数据。

http://www.ipgp.net/api/xml/122.163.6.58

数据是这样来的:

<IpLookup>
    <Ip>122.163.6.58</Ip>
    <Code>IN</Code>
    <Country>India</Country>
    <Flag>http://www.ipgp.net/flags/in.png</Flag>
    <City>Calcutta</City>
    <Region>West Bengal</Region>
    <Isp></Isp>
    <Lat>22.5697</Lat>
    <Lng>88.3697</Lng>
</IpLookup>

任何人都可以建议如何解析并获得结果

4

5 回答 5

2

使用PHP 中包含的XML 解析器?

于 2010-11-16T05:38:39.903 回答
0

我建议您使用xpath这个更简单的模式来访问属性。例如,对于您当前的数据,我有以下内容:

$file = 'file.xml';
$xml = new SimpleXMLElement($file, NULL, TRUE);
$ipz = $xml->xpath("/IpLookup/Ip");
$country = $xml->xpath("/IpLookup/Country/");
foreach($ipz as $ip)
{
    foreach($country as $country)
    {
      echo $ip.'<br/>';
      echo $country.'<br/>';
    }
}

此代码将为您返回当前的ip和。你可以用你自己的方式编辑它。countryxml

于 2012-12-01T08:33:45.550 回答
0

我个人一直在使用这个:http: //ipinfodb.com/

示例非常清晰简洁,API 非常快。

祝你好运。

于 2010-11-16T05:40:00.333 回答
0

使用simplexml_load_string().

于 2010-11-16T05:42:53.617 回答
0

在 PHP 中 API 的使用已经在他们的网站上进行了描述。为什么你使用但不阅读?

http://www.ipgp.net/developer-tools/

于 2010-11-16T06:28:34.627 回答