前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >根据ip地址获取当地天气

根据ip地址获取当地天气

原创
作者头像
MoLeft
发布2022-06-17 08:48:57
1.1K0
发布2022-06-17 08:48:57
举报
文章被收录于专栏:用砖头敲代码用砖头敲代码

获取真实ip

因为有可能用户会使用代理或者其他的手段,所以说我们不能用php自带的函数,我们自定义一个get_real_ip的函数来获取真实ip

代码语言:php
复制
//获取真实ip
function real_ip()
{
    $ip = $_SERVER['REMOTE_ADDR'];
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
        foreach ($matches[0] as $xip) {
            if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                $ip = $xip;
                break;
            }
        }
    } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CF_CONNECTING_IP'])) {
        $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
    } elseif (isset($_SERVER['HTTP_X_REAL_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_IP'])) {
        $ip = $_SERVER['HTTP_X_REAL_IP'];
    }
    return $ip;
}

根据ip获取城市

这里我们肯定不能通过ip段来推算城市了,所以说我们借助一下百度地图的api来获取当前的城市

代码语言:php
复制
//根据ip地址获取城市
function get_ip_city($clientip)
{
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    $url   = 'http://api.map.baidu.com/location/ip?ip=' . $ip . '&ak=zyjHt3iUKWVHB5HjQYy7th1DTbKTPcGY';
    $html  = file_get_contents($url);
    $res   = json_decode($html);
    $cheng = $res->content->address_detail->province; //城市
    $sheng = $res->content->address_detail->city; //省份
    $city  = $cheng . $sheng;
    return $city;
}

根据城市获取天气

ip和城市我们都获取到了,下一步就该获取天气了,这里我用的是今日头条的api,如果你们有别的api也可以自行替换,老规矩get_curl函数自己找

代码语言:php
复制
//获取真实ip
$ip = get_real_ip();
//获取城市
$city = get_ip_city($ip);
//根据城市获取天气
$result = get_curl("https://www.toutiao.com/stream/widget/local_weather/data/?city={$city}");

获取到的东西有点长,我们来看看

代码语言:json
复制
// 20200320181911
// https://www.toutiao.com/stream/widget/local_weather/data/?city=

{
  "data": {
    "city": "吉林",
    "ip": "175.20.xxx.xxx",
    "weather": {
      "aqi": 26,
      "city_name": "吉林",
      "current_condition": "多云",
      "current_temperature": 7,
      "current_time": 1584699547,
      "dat_condition": "多云",
      "dat_high_temperature": 8,
      "dat_low_temperature": -2,
      "dat_weather_icon_id": "1",
      "day_condition": "多云",
      "forecast_list": [
        {
          "condition": "雨夹雪转晴",
          "date": "2020-03-19",
          "high_temperature": "6",
          "low_temperature": "-2",
          "weather_icon_id": "6",
          "wind_direction": "西风",
          "wind_level": "4-5"
        },
      ],
      "high_temperature": 9,
      "hourly_forecast": [
        {
          "condition": "多云",
          "hour": "18",
          "temperature": "7",
          "weather_icon_id": "1",
          "wind_direction": "S",
          "wind_level": "1"
        },
      ],
      "low_temperature": -2,
      "moji_city_id": 189,
      "night_condition": "晴",
      "origin_data": {
        
      },
      "quality_level": "优",
      "tomorrow_aqi": 55,
      "tomorrow_condition": "晴转多云",
      "tomorrow_high_temperature": 8,
      "tomorrow_low_temperature": -3,
      "tomorrow_quality_level": "良",
      "tomorrow_weather_icon_id": "0",
      "update_time": "2020-03-20 18:10:08",
      "weather_icon_id": "1",
      "wind_direction": "南风",
      "wind_level": 1
    }
  }
}

不要慌,我们把它转成数组,然后找到我们需要的天气

代码语言:php
复制
$array = json_decode($result,true);
$weather = $array['data']['weather']['day_condition'];
echo $weather;

到这我们就获取到了我们需要的天气

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 获取真实ip
  • 根据ip获取城市
  • 根据城市获取天气
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档