MENU

根据ip地址获取当地天气

• March 20, 2020 • 教程

这是水文

因为实在是没什么文章可以水的了,但是不发技术文章也不是我的性格,所以说就发一个根据ip获取当地天气的吧。这几天忙着写海报生成的插件,都没时间水文了,不好意思了大家。

获取真实ip

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

//获取真实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来获取当前的城市

//根据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函数自己找

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

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

// 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
    }
  }
}

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

$array = json_decode($result,true);
$weather = $array['data']['weather']['day_condition'];
echo $weather;

到这我们就获取到了我们需要的天气,如果你们有什么更好的api欢迎分享给我

Last Modified: May 26, 2020
Archives 下载海报
QR Code for this page
Tipping QR Code