$ composer require saviorlv/yii2-weather -vvv
在使用本擴展以前,你須要去 高德開放平臺 註冊帳號,而後建立應用,獲取應用的 API Key。php
在config/main.php配置文件中定義component配置信息
'components' => [ ..... 'weather' => [ 'class' => 'Saviorlv\Amap\Weather', 'key' => 'xxxxxxxxx' ], .... ]
$response = Yii::$app->weather->getLiveWeather('深圳');
示例:git
{ "status": "1", "count": "1", "info": "OK", "infocode": "10000", "lives": [ { "province": "廣東", "city": "深圳市", "adcode": "440300", "weather": "中雨", "temperature": "27", "winddirection": "西南", "windpower": "5", "humidity": "94", "reporttime": "2018-08-21 16:00:00" } ] }
$response = Yii::$app->weather->getForecastsWeather('深圳');
示例:github
{ "status": "1", "count": "1", "info": "OK", "infocode": "10000", "forecasts": [ { "city": "深圳市", "adcode": "440300", "province": "廣東", "reporttime": "2018-08-21 11:00:00", "casts": [ { "date": "2018-08-21", "week": "2", "dayweather": "雷陣雨", "nightweather": "雷陣雨", "daytemp": "31", "nighttemp": "26", "daywind": "無風向", "nightwind": "無風向", "daypower": "≤3", "nightpower": "≤3" }, { "date": "2018-08-22", "week": "3", "dayweather": "雷陣雨", "nightweather": "雷陣雨", "daytemp": "32", "nighttemp": "27", "daywind": "無風向", "nightwind": "無風向", "daypower": "≤3", "nightpower": "≤3" }, { "date": "2018-08-23", "week": "4", "dayweather": "雷陣雨", "nightweather": "雷陣雨", "daytemp": "32", "nighttemp": "26", "daywind": "無風向", "nightwind": "無風向", "daypower": "≤3", "nightpower": "≤3" }, { "date": "2018-08-24", "week": "5", "dayweather": "雷陣雨", "nightweather": "雷陣雨", "daytemp": "31", "nighttemp": "26", "daywind": "無風向", "nightwind": "無風向", "daypower": "≤3", "nightpower": "≤3" } ] } ] }
以上兩個方法第二個參數爲返回值類型,可選json
與xml
,默認json
:
$response = $weather->getLiveWeather('深圳', 'xml');
示例:web
<response> <status>1</status> <count>1</count> <info>OK</info> <infocode>10000</infocode> <lives type="list"> <live> <province>廣東</province> <city>深圳市</city> <adcode>440300</adcode> <weather>中雨</weather> <temperature>27</temperature> <winddirection>西南</winddirection> <windpower>5</windpower> <humidity>94</humidity> <reporttime>2018-08-21 16:00:00</reporttime> </live> </lives> </response>
array | string getLiveWeather(string $city, string $format = 'json') array | string getForecastsWeather(string $city, string $format = 'json')
$city
- 城市名/高德地址位置 adcode,好比:「深圳」 或者(adcode:440300);$format
- 輸出的數據格式,默認爲 json 格式,當 output 設置爲 「xml
」 時,輸出的爲 XML 格式的數據。
非誠感謝 overtrue/weather
MITjson