php获取天气

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013939884/article/details/72636569
public function getWeather()
    {
        //方法1:
        $url = 'http://wthrcdn.etouch.cn/weather_mini?city=北京';
        $html = file_get_contents($url);
        echo gzdecode($html);
        // 方法2
        $ch = curl_init();
        $timeout = 5;
        curl_setopt($ch, CURLOPT_URL, 'http://wthrcdn.etouch.cn/weather_mini?city=北京');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//        curl_setopt($ch,CURLOPT_ENCODING ,'gzip');
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        $file_contents = curl_exec($ch);
        curl_close($ch);

        echo gzdecode($file_contents);
    }

猜你喜欢

转载自blog.csdn.net/u013939884/article/details/72636569
今日推荐