18910140161

tp5 根据ip获得当前城市天气预报非iframe

2021-08-17 17:12:57

242

今天做项目,有个需求网站根据用户访问自动获取当地的天气预报,其实刚开始觉得这个功能超级简单,直接iframe一个页面不就行了么。但是iframe的基本都有别人的链接,那么今天我就给大家分享一个比较的方法吧!

首先规划一下思路,这个需求应该分成两步根据ip获取城市名称,第二步更具城市名称获取城市天气预报。好下面我们具体操作:

步:根据ip获取城市名称

这里我们用太平洋的公共接口(主要是免费)http://whois.pconline.com.cn/ 这是接口说明

/*
 * 太平洋根据ip获取城市名字
*/
function getCityFormIp($url)
{
    $cityName = @file_get_contents($url);
    $cityName = iconv('GBK//IGNORE', 'UTF-8', $cityName);
    $cityName = preg_replace("/\s+/", "", $cityName);
    $cityName = json_decode($cityName);
    if ($cityName->err == '' || $cityName->city == '') {
        $city = '北京';
    }else{
        $city = str_replace('市','',$cityName->city);
    }
    return $city;
}

这里我做了下处理如果没获取到 默认城市就是北京,可以根据自己的情况修改,因为后面我们的天气预报接口需要用到城市,但是不能有市这个字所以过滤掉先,这个只是获取的方法下面我们做一下参数的处理

public function getWeather()
    {
        $ip = $_SERVER["REMOTE_ADDR"];
        $url = "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=".$ip;
        $city = getCityFormIp($url);
        $today = strtotime(date('Y-m-d'));
        $weather = [];
        // 是否获取到了城市名称
        if ($city) {
            // cookies里是否有天气信息
            $cookieWeather = cookie($city);
            if ($cookieWeather) {
                // cookie里的信息是否是昨天的
                $ckWeather = encryption($cookieWeather,1);
                if ($ckWeather['time'] < $today) {
                    // 如果是昨天的那么就重新获取
                    $weather = $this->_getWeather($city);
                }else{
                    // 否则直接将cookie里的信息给$weather                    
                    $weather = $ckWeather;
                }
            }else{
                $weather = $this->_getWeather($city);
            }            
        }
        foreach ($weather as $k => $v) {
            if ($k == 'wid') {
                $weather['cnwid'] = cnwid($v);
            }
        }
        return $weather;
    }
    public function _getWeather($city)
    {
        // 请求的接口URL
        $apiUrl = 'http://apis.juhe.cn/simpleWeather/query';
        $weatherkey = $ipkey = $this->config['juhe_weather'];
        // 请求参数
        $params = [
            'city' => $city, // 要查询的城市
            'key' => $weatherkey,
        ];
        $paramsString = http_build_query($params);
        // 发起接口网络请求
        $response = juheHttpRequest($apiUrl, $paramsString , 1);
        $result = json_decode($response, true);
        if ($result['error_code'] == 0) {
            $weather['code'] = 0;
            $weather['data'] = $result['result']['realtime'];
            $weather['data']['city'] = $result['result']['city'];
            $weather['data']['time'] = time();
            $jsonData = encryption($weather['data'],0);
            cookie($result['result']['city'], $jsonData);
        }else{
            $weather['code'] = 1;
            $weather['data'] = '';
        }
        return $weather;
    }

因为获取ip的api是免费的,获取天气预报的是 收费的所以我将每天次获取到的天气预报的数据放到了cookies里面,这样就不用,同一个用户每天重复扣费的问题,也提升了速度,cookies这里用到了序列化和base64加密,这样别人就看不到具体的内容了,天气的具体查询方法就不贴出来了,聚合api的说明文档里有原封不动的复制过来就可以了,好了今天就分享到这里,感谢各位的浏览。

我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航