PHP获取当天和72小时天气预报,并生成接口
程序员文章站
2022-05-27 11:55:29
...
参数:
city,城市ID,详细请见下表,比如:212为重庆
type,输出格式,json或xml,默认为json
charset,输出字符编码,utf-8或utf8或gbk,默认为utf-8
datetype,日期输出类型,unix或other,默认为unix时间戳
如:getweather.php? type=xml& city=212
原理:
读取http://weather.news.qq.com/inc/07_dc' . $city . '.htm的HTML代码,通过正则检索出结果。
城市列表:
1 香港
2 澳门
17 哈尔滨
28 乌鲁木齐
56 西宁
57 兰州
69 呼和浩特
78 银川
82 石家庄
84 太原
103 长春
115 沈阳
125 北京
127 天津
140 济南
150 拉萨
166 成都
179 昆明
186 西安
189 郑州
211 武汉
212 重庆
218 长沙
227 贵阳
232 桂林
244 南京
248 合肥
252 上海
255 杭州
264 南昌
276 福州
280 台北
287 厦门
292 广州
295 南宁
296 深圳
303 海口
getweather.php
<?php include('./include/encode.func.php'); $city = $_GET['city'] ? intval($_GET['city']) : 212; //cq $type = $_GET['type'] ? strtolower(trim($_GET['type'])) : 'json'; //json $charset = $_GET['charset'] ? strtolower(trim($_GET['charset'])) : 'utf-8'; //charset $dateType = $_GET['datetype'] ? strtolower(trim($_GET['datetype'])) : 'unix'; //dateType if($city < 1) exit(); /*Get Weather*/ $content = ''; $fp = fopen('http://weather.news.qq.com/inc/07_dc' . $city . '.htm','r'); while(!feof($fp)) $content .= fread($fp,1024); fclose($fp); $content = str_replace(array("\t","\r","\n"),'',$content); $content = anystring2utf8($content); $weather = array(); $a = array(); preg_match('/class="px14\stred"><strong>(.[^\&]*)\ (.[^<]*)<\/strong>/i',$content,$a); $weather['area'] = trim($a[2]); $weather['now']['date'] = parseDate($a[1]); preg_match('/<img\swidth="64"\sheight="64"\ssrc="(.[^"]*)"/i',$content,$a); $weather['now']['pic'] = trim($a[1]); preg_match('/bgcolor="#EDF2F8" class="blues">(.[^<]*)<br>(.[^<]*)<\/td>/i',$content,$a); $weather['now']['weather'] = trim($a[1]); $weather['now']['temp'] = trim($a[2]); preg_match('/class="explain\sblues">(.[^<]*)<br>\s*(.[^<]*)<br>\s*(.[^<]*)</i',$content,$a); $weather['now']['wind'] = parseMore($a[1]); $weather['now']['uv'] = parseMore($a[2]); $weather['now']['air'] = parseMore($a[3]); preg_match_all('/bgcolor="#EEEEEE">(.[^<]*)<\/td>(.[^-]*)bgcolor="#EEF3F8">(.[^<]*)<br>(.[^<]*)<br>(.[^<]*)/i',$content,$a); foreach($a[0] as $k => $v){ $weather['future72'][$k] = array( 'date' => $dateType == 'unix' ? strtotime(trim($a[1][$k])) : trim($a[1][$k]), 'weather' => trim($a[3][$k]), 'temp' => trim($a[4][$k]), 'wind' => trim($a[5][$k]), 'pic' => parsePic($a[2][$k]) ); } /*Get exponent*/ $weather['exponent'] = array(); $content = ''; $fp = fopen('http://weather.news.qq.com/inc/07_zs' . $city . '.htm','r'); while(!feof($fp)) $content .= fread($fp,1024); fclose($fp); $content = str_replace(array("\t","\r","\n"),'',$content); $content = anystring2utf8($content); preg_match_all('/<strong>(.[^<]*)<\/strong>:<span\sclass="tred">(.[^<]*)<\/span>.[^:]*width="180">(.[^<]*)<\/td>/i',$content,$a); foreach($a[0] as $k => $v) { $weather['exponent'][$k] = array( 'name' => trim($a[1][$k]), 'value' => trim($a[2][$k]), 'memo' => trim($a[3][$k]) ); } /*Print Result*/ if ($charset != 'utf-8' && $charset != 'utf8') $weather = any2gbk($weather); switch($type) { case 'json': echo json_encode($weather); break; case 'xml': header("content-type:text/xml"); $ax = new array2xml($weather,$charset); echo $ax->getXML(); break; default: echo '<pre>'; print_r($weather); echo '</pre>'; break; } function parseDate($date) { $str = $date; $str = preg_replace('/([^\d])/',' ',$str); $str = trim($str); $str = str_replace(' ','-',$str); return $dateType == 'unix' ? strtotime($str) : $str; } function parseMore($str){ $str = trim($str); $tmp = explode(iconv('gbk','UTF-8',':'),$str); return $tmp[1]; } function parsePic($str) { $a = array(); preg_match_all('/src="(.[^"]*)"/i',$str,$a); $result = $a[1]; return $result; } class array2xml { var $xml; function array2xml($array,$encoding='gbk') { $this->xml='<?xml version="1.0" encoding="'.$encoding.'"?>'; if(count($array) > 1) { $array = array('catalog' => $array); } $this->xml .= $this->_array2xml($array); } function getXml() { return $this->xml; } function _array2xml($array) { foreach($array as $key=>$val) { is_numeric($key) && $key = "item id=\"$key\""; $xml.= "<$key>"; $xml.= is_array($val) ? $this->_array2xml($val) : $val; list($key,) = explode(' ',$key); $xml .= "</$key>"; } return $xml; } } ?>
encode.func.php
<?php /** * 任何编码字符串转换为utf-8 * * @param string $str 输入字符串 * @return string 输出utf-8编码字符串 */ function anystring2utf8($str) { $encode = mb_detect_encoding($str,"ASCII,UNICODE,UTF-8,GBK,CP936,EUC-CN,BIG-5,EUC-TW"); return ($encode != 'UTF-8' && $encode != 'ASCII' ? iconv($encode,'UTF-8',$str) : $str); } /** * 任何编码字符串转换为gbk * * @param string $str 输入字符串 * @return string 输出gbk编码字符串 */ function anystring2gbk($str) { $encode = mb_detect_encoding($str,"ASCII,UNICODE,UTF-8,GBK,CP936,EUC-CN,BIG-5,EUC-TW"); return ($encode != 'CP936' && $encode != 'ASCII' && $encode != 'GBK' ? iconv($encode,'GB18030',$str) : $str); } /** * 任何编码字符串(数组)转换为utf-8 * * @param mixed $string 输入字符串(数组) * @return mixed 输出utf-8编码字符串(数组) */ function any2utf8($string) {//通过递归转换字符串编码 if(is_array($string)) { foreach($string as $key => $val) { $string[$key] = any2utf8($val); //递归 } } else { $string = anystring2utf8($string); } return $string; } /** * 任何编码字符串(数组)转换为gbk * * @param mixed $string 输入字符串(数组) * @return mixed 输出gbk编码字符串(数组) */ function any2gbk($string) {//通过递归转换字符串编码 if(is_array($string)) { foreach($string as $key => $val) { $string[$key] = any2gbk($val); //递归 } } else { $string = anystring2gbk($string); } return $string; } ?>