第三天气接口使用总结

使用第三天气接口:

 1 <?php
 2 /**
 3  * Created by PhpStorm.
 4  * User: Lin
 5  * Date: 17/9/19
 6  * Time: 下午8:17
 7  */
 8 
 9 namespace HomeModel;
10 use ThinkModel;
11 
12 class IndexModel extends Model {
13     // https://free-api.heweather.com/v5/weather?city=yourcity&key=yourkey
14     //$key = "32e79235a2bb41c4b49a4fc59d116b5d";
15     protected $key;
16     protected $url;
17     public function __construct()
18     {
19         $this->key = "yourkey";  //使用自己申请的appkey
20         $this->url = "https://free-api.heweather.com/v5/";
21     }
22 
23 
24     /**
25      * @param $weatherType 获取天气数据类型
26      * @param $city 城市中英文名称、ID、IP和经纬度
27      */
28     public function getWeatherByCity($weatherType,$city) {
29         $url = $this->url . $weatherType . '?' . 'city=' . $city . '&key=' . $this->key;
30         $res = (file_get_contents($url));
31         return (json_decode($res,true));
32     }
33 }
<?php
namespace HomeController;

use ThinkController;

class IndexController extends Controller
{
    public function index()
    {
        $m = D('Index');
        $data = $m->getWeatherByCity('weather', '杭州');
        $data = $data['HeWeather5'][0];

        $this->assign('data', $data);
//        $this->assign('data',"111111");
        $this->display();
    }

}
原文地址:https://www.cnblogs.com/linst/p/7568730.html