第三天气接口使用总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第三天气接口使用总结相关的知识,希望对你有一定的参考价值。
使用第三天气接口:
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(); } }
以上是关于第三天气接口使用总结的主要内容,如果未能解决你的问题,请参考以下文章