php根据IP获取所在省份-百度api接口

这里用的file_put_contents,你也可以用别的,直接怼代码:

//拼接传递的参数
    $getData = array(
        'query'    =>    '127.0.0.1',
        'resource_id'    =>    '6006',
        'oe'    =>    'UTF-8',
        'format'    =>    'json'
    );
    $getData = http_build_query($getData);
    $opts = array(
      'http'=>array(
        'method'=>"POST",
        'header'=>"Content-type:application/x-www-form-urlencoded\r\n" .
                  "Content-length:". strlen($getData) ."\r\n",
                  "content"=>$getData,
      )
    );
    //获取Ip所属位置
    $context = stream_context_create($opts);
    $fp = file_get_contents('http://opendata.baidu.com/api.php', false, $context);
    var_dump(json_decode($fp, true));

返回的数据样式为:

array (4) { ["status"]=> string(1) "0" ["t"]=> string(0) "" ["set_cache_time"]=> string(0) "" ["data"]=> array(1) { [0]=> array (15) { ["location"]=> string(31) "北京市北京市 不告诉你" ["titlecont"]=> string(14) "IP地址查询" ["origip"]=> string(14) "123.206.79.176" ["origipquery"]=> string(14) "123.206.79.176" ["showlamp"]=> string(1) "1" ["showLikeShare"]=> int(1) ["shareImage"]=> int(1) ["ExtendedLocation"]=> string(0) "" ["OriginQuery"]=> string(14) "123.206.79.176" ["tplt"]=> string(2) "ip" ["resourceid"]=> string(4) "6006" ["fetchkey"]=> string(14) "123.206.79.176" ["appinfo"]=> string(0) "" ["role_id"]=> int(0) ["disp_type"]=> int(0) } } } 

 

转载于:https://www.cnblogs.com/wxdblog/p/8884870.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据百度API获取经纬度对应的省市区信息,可以使用Python进行实现。以下是一个示例代码: ```python import requests def get_location_info(latitude, longitude): ak = '你的百度开发者API密钥' # 需要替换为自己的百度开发者API密钥 url = f'http://api.map.baidu.com/reverse_geocoding/v3/?ak={ak}&output=json&coordtype=wgs84ll&location={latitude},{longitude}' response = requests.get(url) data = response.json() if data['status'] == 0: province = data['result']['addressComponent']['province'] city = data['result']['addressComponent']['city'] district = data['result']['addressComponent']['district'] return province, city, district else: return None # 使用示例 latitude = 39.9087 # 纬度 longitude = 116.3975 # 经度 location_info = get_location_info(latitude, longitude) if location_info: province, city, district = location_info print(f'该经纬度所在位置为:省份:{province},城市:{city},区县:{district}') else: print('获取位置信息失败') ``` 以上代码中,我们使用了Python的requests库发送HTTP请求,并通过百度API的逆地理编码接口获取经纬度对应的位置信息。需要注意,你需要将`ak`变量的值替换为自己的百度开发者API密钥。 具体步骤为: 1. 引入requests库,用于发送HTTP请求。 2. 定义一个`get_location_info`函数,该函数接受经度和纬度作为参数,并返回省市区的信息。 3. 在函数中构建请求URL,包括百度API密钥、经纬度等信息,并发送GET请求获取响应数据。 4. 解析API返回的JSON数据,提取省市区信息。 5. 在主程序中,调用`get_location_info`函数,传入经纬度参数,获取位置信息。 6. 如果成功获取位置信息,则将其打印出来;否则输出获取位置信息失败的提示。 注意,以上代码仅为示例,具体的实现需要根据你所使用的具体百度API接口和参数进行适当的调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值