php sina ip api,新浪接口ip获取省市【原创】

在最近项目中使用百度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

75d087ef9a9fb11dc373caaf33adbf7f.png

微信打赏

支付宝打赏

感谢您对作者Leon的打赏,我们会更加努力!    如果您想成为作者,请点我

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值