PHP code example of run6 / weather
1. Go to this page and download the library: Download run6/weather library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
run6 / weather example snippets
use Run6\Weather\Weather;
$key = 'xxxxxxxxxxxxxxx'; //替换成你的API—KEY
$weather = new Weather($key);
$response = $weather->getWeather('杭州');
$response = $weather->getWeather('杭州','all')
$response = $weather('杭州','base','xml')
array|string getWeather(string $city, string $type = 'base', string $format = 'json')
.
.
.
'weather' => [
'key' => env('WEATHER_API_KEY'),
]
WEATHER_API_KEY=xxxxxxxxx
·
·
·
// 在控制器中通过依赖注入的方式
public function getWeather(Weather $weather)
{
$response = $weather->getWeather('杭州');
}
.
.
.
.
.
.
pubcli function getWeather()
{
$response = app('weather')->getWeather('杭州');
}
.
.
.
json
{
"status": "1",
"count": "1",
"info": "OK",
"infocode": "10000",
"forecasts": [
{
"city": "杭州市",
"adcode": "330100",
"province": "浙江",
"reporttime": "2019-06-10 13:51:19",
"casts": [
{
"date": "2019-06-10",
"week": "1",
"dayweather": "小雨",
"nightweather": "多云",
"daytemp": "33",
"nighttemp": "21",
"daywind": "东北",
"nightwind": "东北",
"daypower": "≤3",
"nightpower": "≤3"
},
{
"date": "2019-06-11",
"week": "2",
"dayweather": "多云",
"nightweather": "多云",
"daytemp": "29",
"nighttemp": "21",
"daywind": "东北",
"nightwind": "东北",
"daypower": "≤3",
"nightpower": "≤3"
},
{
"date": "2019-06-12",
"week": "3",
"dayweather": "多云",
"nightweather": "多云",
"daytemp": "29",
"nighttemp": "21",
"daywind": "东",
"nightwind": "东",
"daypower": "4",
"nightpower": "4"
},
{
"date": "2019-06-13",
"week": "4",
"dayweather": "小雨",
"nightweather": "晴",
"daytemp": "29",
"nighttemp": "18",
"daywind": "无风向",
"nightwind": "无风向",
"daypower": "≤3",
"nightpower": "≤3"
}
]
}
]
}
xml
<response>
<status>1</status>
<count>1</count>
<info>OK</info>
<infocode>10000</infocode>
<lives type="list">
<live>
<province>浙江</province>
<city>杭州市</city>
<adcode>330100</adcode>
<weather>多云</weather>
<temperature>33</temperature>
<winddirection>东</winddirection>
<windpower>≤3</windpower>
<humidity>50</humidity>
<reporttime>2019-06-10 13:51:19</reporttime>
</live>
</lives>
</response>