我需要获取用户的当前位置并将其保存到数据库。我有这个小的代码片段在PHP和Js从ipinfo.io通过ip地址获取用户位置 - ipinfo.io
但PHP代码不起作用,js版本。
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $details->city; // -> outputs nothing
<script>
$.get("http://ipinfo.io", function(response) {
console.log(response.city);//logged my current city
}, "jsonp");
</script>
任何想法如何实现PHP版本?
更新
我试图
var_dump($details)
显示我
object(stdClass)[1]
public 'ip' => string '::1' (length=3)
public 'hostname' => string 'localhost' (length=9)
public 'city' => null
public 'country' => string 'AU' (length=2)
public 'loc' => string '-27.0000,133.0000' (length=17)
我在本地主机测试它与我的国家是菲律宾
可能重复[如何从PHP中的ipinfo.io中获取位置?](http://stackoverflow.com/questions/28012011/how-to-obtain-location-from-ipinfo-io-in-php) – chris85
'var_dump($ details)'并检查城市的价值。 – Harikrishnan