【发布时间】:2011-12-16 20:07:48
【问题描述】:
我正在尝试使用外部站点的 API 通过file_get_contents 获取用户 IP 信息(国家、城市等):
$ht="http://api.hostip.info/?ip=99.99.99.99";//example ip address<<
$com = file_get_contents("$ht");
preg_match('/<countryName>(.*)<\/countryName>/',$com,$oun);
preg_match('/<gml:name>(.*)<\/gml:name>/',$com,$it);
$country=$oun[1];
$city=$it[1];
echo $country, $city;
我可以使用该代码获取国家、IP 和其他信息,但不能获取城市...
我认为问题在于 :
在gml:name....
这是我试图从中获取数据的 XML:
【问题讨论】:
-
使用适当的 XML 解析器代替正则表达式怎么样?
-
为什么不用
simplexml来阅读呢?
标签: php preg-match file-get-contents