【发布时间】:2016-08-21 04:10:58
【问题描述】:
我需要获取用户的当前位置并将其保存到数据库中。我在 ipinfo.io 的 PHP 和 Js 中有这个小的 sn-p
但是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)
我在 localhost 测试它,我的国家是菲律宾
【问题讨论】:
-
var_dump($details)并检查城市的值。
标签: php geolocation