在最近项目中使用百度API商店找到的接口来通过ip地址获取省市, 用了一段时间过后竟然提示过期了, 最后还是选择使用新浪接口
public function sinaIpQueryAddress($ip)
{
$ch = curl_init("http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip={$ip}");
//curl_setopt($ch,CURLOPT_ENCODING ,’utf8′);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 获取数据返回
$location = json_decode(curl_exec($ch));
curl_close($ch);
if (!is_object($location) || !isset($location->ret) || $location->ret == -1) {
return [
‘status’ => 0,
‘info’ => ‘获取地理位置失败’
];
}
return [
‘status’ => 1,
‘info’ => ‘获取地理位置成功’,
‘handle’ => $location
];
}
获得结果大致为
array (size=3)
'status' => int 1
'info' => string '获取地理位置成功' (length=16)
'handle' =>
object(stdClass)[2]
public 'ret' => int 1
public 'start' => int -1
public 'end' => int -1
public 'country' => string '中国' (length=6)
public 'province' => string '安徽' (length=6)
public 'city' => string '芜湖' (length=6)
public 'district' => string '' (length=0)
public 'isp' => string '' (length=0)
public 'type' => string '' (length=0)
public 'desc' => string '' (length=0)
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/20304.html
微信打赏
支付宝打赏
感谢您对作者Leon的打赏,我们会更加努力! 如果您想成为作者,请点我