武汉未来90天天气预报查询 (武汉未来90天天气预报最新)

武汉休闲 05-24 阅读:79 评论:0
' . $weather_data['error'] . ' ';} else {// 输出天气预报表echo '';echo '';echo '';echo '';echo '';echo '';echo '';echo '';echo '';foreach ($weather_data as $date => $weather) {echo '';echo '';echo '';echo '';echo '';}echo '';echo ' 武汉未来90天天气预报查询 (武汉未来90天天气预报最新)
日期 天气 温度
' . $date . ' ' .$weather['weather'] . ' ' . $weather['temperature'] . '
';}// 获取天气预报数据函数function get_weather_data($dates) {// 使用第三方API获取天气预报数据$api_key = 'YOUR_API_KEY';$api_url = 'https://api.openweathermap.org/data/2.5/forecast/daily?q=武汉&cnt=90&appid=' . $api_key;// 初始化cURL$curl = curl_init();// 设置cURL选项curl_setopt_array($curl, array(CURLOPT_URL => $api_url,CURLOPT_RETURNTRANSFER => true,CURLOPT_SSL_VERIFYPEER => false,));// 执行cURL请求$response = curl_exec($curl);// 检查cURL错误if (curl_errno($curl)) {return array('error' => 'cURL error: ' . curl_error($curl));}// 关闭cURLcurl_close($curl);// 解析JSON响应$data = json_decode($response, true);// 检查API错误if (isset($data['cod']) && $data['cod'] != 200) {return array('error' => 'API error: ' . $data['message']);}// 提取天气预报数据$weather_data = array();foreach ($data['list'] as $item) { $date = date('Y-m-d', $item['dt']);$weather_data[$date] = array('weather' => $item['weather'][0]['main'],'temperature' => $item['temp']['day'] . '°C',);}// 返回天气预报数据return $weather_data;}?>
版权声明

本文仅代表作者观点,不代表武汉桑拿立场。
本文系作者授权发表,未经许可,不得转载。