PHP:通过IP地址获取城市

时间:2015-08-10 20:51:19

标签: php regex web-scraping

好的,所以我研究了寻找IP地址和抓取所以我需要一种方法来获得进入我的网站的城市,它会查看某些数据。

任何关于我做错了什么的想法,现在我只需要它来回应这个城市

所以这是我的代码

//Reach Site

$ipsite=file_get_contents("http://whatismyipaddress.com/ip/".$_SERVER['REMOTE_ADDR']);

//Scrape City

preg_match("/<th>City:</th><td>(.*?)</s", $ipsite, $ipcity);

//echo city

echo($ipcity[1]);

非常感谢

1 个答案:

答案 0 :(得分:3)

我会使用此网站API来获取位置。

ipinfo

您可以通过以下方式使用PHP执行此操作。

$ip = $_SERVER['REMOTE_ADDR'];
$result = json_decode(file_get_contents("http://ipinfo.io/{$ip}"));
echo $result->city;
相关问题