/**
* 天气
* @param string $city
* @return array
*/
public function weather($city = '合肥')
{
//天气接口URL
$urls = "http://wthrcdn.etouch.cn/weather_mini?city=" . $city;
//得到URL中的内容
$contents = file_get_contents("compress.zlib://" . $urls);
//转化为json
$data = json_decode($contents, true);
$result['city'] = $data['data']['city'];
$date = ['今天', '明天', '后天'];
$patterns = "/\d+/";//结合正则获取字符串中数字
foreach ($data['data']['forecast'] as $key => $value) {
if ($key <= 2) {
$low = explode(' ', $value['low']);
$high = explode(' ', $value['high']);
preg_match_all($patterns, $value['fengli'], $fengli);
if (count($fengli[0]) == 1) {
$feng = $fengli[0][0];
} else {
$feng = ($fengli[0][0] > $fengli[0][1]) ? $fengli[0][0] : $fengli[0][1];
}
$result['list'][] = [
'date' => $date[$key],
'type' => $value['type'],
'low' => $low[1],
'high' => $high[1],
'fengxiang' => $value['fengxiang'],
'fengli' => $feng . '级'
];
}
}
$result['today'] = $result['list'][0];
return hello_success('ok', $result);
}
12-10
361
12-14
531