PHP code example of overtest / weather
1. Go to this page and download the library: Download overtest/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/ */
overtest / weather example snippets `
$response = $weather->getWeather('深圳');
示例:
{
"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"
}
]
}
.
.
.
public function edit(Weather $weather)
{
$response = $weather->getWeather('深圳');
}
.
.
.
$xslt
.
.
.
public function edit()
{
$response = app('weather')->getWeather('深圳');
}
.
.
.
.
public function getWeather($city, $type = 'live', $format = 'json')
{
$url = 'https://restapi.amap.com/v3/weather/weatherInfo';
$types = [
'live' => 'base',
'forecast' => 'all',
];
if (!\in_array(\strtolower($format), ['xml', 'json'])) {
throw new InvalidArgumentException('Invalid response format: '.$format);
}
if (!\array_key_exists(\strtolower($type), $types)) {
throw new InvalidArgumentException('Invalid type value(live/forecast): '.$type);
}
.
.
.
}
angular2html
.
.
public function getLiveWeather($city, $format = 'json')
{
return $this->getWeather($city, 'base', $format);
}
public function getForecastsWeather($city, $format = 'json')
{
return $this->getWeather($city, 'all', $format);
}
.
.