首页 > 其他 > 详细

第三天气接口使用总结

时间:2017-09-21 15:15:42      阅读:246      评论:0      收藏:0      [点我收藏+]

使用第三天气接口:

 1 <?php
 2 /**
 3  * Created by PhpStorm.
 4  * User: Lin
 5  * Date: 17/9/19
 6  * Time: 下午8:17
 7  */
 8 
 9 namespace Home\Model;
10 use Think\Model;
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 Home\Controller;

use Think\Controller;

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();
    }

}

 

第三天气接口使用总结

原文:http://www.cnblogs.com/linst/p/7568730.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!