zoukankan      html  css  js  c++  java
  • 第三天气接口使用总结

    使用第三天气接口:

     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();
        }
    
    }
  • 相关阅读:
    临时表的问题
    List集合和Set集合互转
    mysql数据库事件
    mysql存储过程事务
    N皇后问题
    递归实现字符数组的全排列及组合
    判断一个序列是否为某二叉搜索树的后续遍历结果
    递归实现两个有序链表的合并
    递归实现字符串反转
    根据字节数截取字符串
  • 原文地址:https://www.cnblogs.com/linst/p/7568730.html
Copyright © 2011-2022 走看看