[英]Getting users location by curl
我很难通过Curl定位用户位置。
我有这个代码:
$ip ='123.125.114.144';
$result ='Unkown' ;
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, 'http://www.iptolatlng.com/?ip='.$ip);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$ip_data = curl_exec($ch);
curl_close($ch);
if($ip_data && $ip_data->countryFullName != null) //---the error is in this line
{
$result = $ip_data->countryFullName;
}
echo $result;
但是即时通讯收到此错误:
注意:尝试在第29行的/mywebsite/index.php中获取非对象的属性未知
出于测试目的,这是他们与随机IP的链接http://www.iptolatlng.com/?ip=123.125.114.144
对于出了什么问题,任何帮助都将不胜枚举。
当您尝试访问对象类型的结果值时, curl_exec
以字符串类型返回请求的结果。 因为您的请求返回json字符串,所以您应该使用json_decode
函数。
例:
$ip_data = curl_exec($ch);
$ip_obj = json_decode($ip_data);
echo $ip_obj->countryFullName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.