Rumah > php教程 > PHP源码 > PHP获取当天和72小时天气预报,并生成接口

PHP获取当天和72小时天气预报,并生成接口

PHP中文网
Lepaskan: 2016-05-25 17:11:39
asal
1590 orang telah melayarinya

参数: 

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 海口

11.png

getweather.php

<?php
    include(&#39;./include/encode.func.php&#39;);
     
    $city = $_GET[&#39;city&#39;] ? intval($_GET[&#39;city&#39;]) : 212; //cq
    $type = $_GET[&#39;type&#39;] ? strtolower(trim($_GET[&#39;type&#39;])) : &#39;json&#39;; //json
    $charset = $_GET[&#39;charset&#39;] ? strtolower(trim($_GET[&#39;charset&#39;])) : &#39;utf-8&#39;; //charset
    $dateType = $_GET[&#39;datetype&#39;] ? strtolower(trim($_GET[&#39;datetype&#39;])) : &#39;unix&#39;; //dateType
     
    if($city < 1)
        exit();
     
    /*Get Weather*/
    $content = &#39;&#39;;
    $fp = fopen(&#39;http://weather.news.qq.com/inc/07_dc&#39; . $city . &#39;.htm&#39;,&#39;r&#39;);
    while(!feof($fp))
        $content .= fread($fp,1024);
    fclose($fp);
     
    $content = str_replace(array("\t","\r","\n"),&#39;&#39;,$content);
    $content = anystring2utf8($content);
    $weather = array();
     
    $a = array();
    preg_match(&#39;/class="px14\stred"><strong>(.[^\&]*)\&#160;(.[^<]*)<\/strong>/i&#39;,$content,$a);
    $weather[&#39;area&#39;] = trim($a[2]);
    $weather[&#39;now&#39;][&#39;date&#39;] = parseDate($a[1]);
     
    preg_match(&#39;/<img\swidth="64"\sheight="64"\ssrc="(.[^"]*)"/i&#39;,$content,$a);
    $weather[&#39;now&#39;][&#39;pic&#39;] = trim($a[1]);
    preg_match(&#39;/bgcolor="#EDF2F8" class="blues">(.[^<]*)<br>(.[^<]*)<\/td>/i&#39;,$content,$a);
    $weather[&#39;now&#39;][&#39;weather&#39;] = trim($a[1]);
    $weather[&#39;now&#39;][&#39;temp&#39;] = trim($a[2]);
    preg_match(&#39;/class="explain\sblues">(.[^<]*)<br>\s*(.[^<]*)<br>\s*(.[^<]*)</i&#39;,$content,$a);
    $weather[&#39;now&#39;][&#39;wind&#39;] = parseMore($a[1]);
    $weather[&#39;now&#39;][&#39;uv&#39;] = parseMore($a[2]);
    $weather[&#39;now&#39;][&#39;air&#39;] = parseMore($a[3]);
     
    preg_match_all(&#39;/bgcolor="#EEEEEE">(.[^<]*)<\/td>(.[^-]*)bgcolor="#EEF3F8">(.[^<]*)<br>(.[^<]*)<br>(.[^<]*)/i&#39;,$content,$a);
     
    foreach($a[0] as $k => $v){
        $weather[&#39;future72&#39;][$k] = array(
            &#39;date&#39; => $dateType == &#39;unix&#39; ? strtotime(trim($a[1][$k])) : trim($a[1][$k]),
            &#39;weather&#39; => trim($a[3][$k]),
            &#39;temp&#39; => trim($a[4][$k]),
            &#39;wind&#39; => trim($a[5][$k]),
            &#39;pic&#39; => parsePic($a[2][$k])
        );
    }
    /*Get exponent*/
    $weather[&#39;exponent&#39;] = array();
    $content = &#39;&#39;;
    $fp = fopen(&#39;http://weather.news.qq.com/inc/07_zs&#39; . $city . &#39;.htm&#39;,&#39;r&#39;);
    while(!feof($fp))
        $content .= fread($fp,1024);
    fclose($fp);
    $content = str_replace(array("\t","\r","\n"),&#39;&#39;,$content);
    $content = anystring2utf8($content);
    preg_match_all(&#39;/<strong>(.[^<]*)<\/strong>:<span\sclass="tred">(.[^<]*)<\/span>.[^:]*width="180">(.[^<]*)<\/td>/i&#39;,$content,$a);
    foreach($a[0] as $k => $v) {
        $weather[&#39;exponent&#39;][$k] = array(
            &#39;name&#39; => trim($a[1][$k]),
            &#39;value&#39; => trim($a[2][$k]),
            &#39;memo&#39; => trim($a[3][$k])
        );
    }
     
    /*Print Result*/
    if ($charset != &#39;utf-8&#39; && $charset != &#39;utf8&#39;)
        $weather = any2gbk($weather);
         
    switch($type) {
        case &#39;json&#39;:
            echo json_encode($weather);
            break;
        case &#39;xml&#39;:
            header("content-type:text/xml");
            $ax = new array2xml($weather,$charset);
            echo $ax->getXML();
            break;
        default:
            echo &#39;<pre class="brush:php;toolbar:false">&#39;;
            print_r($weather);
            echo &#39;
'; 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=''; 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 .= ""; } return $xml; } } ?>
Salin selepas log masuk

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 != &#39;UTF-8&#39; && $encode != &#39;ASCII&#39; ? iconv($encode,&#39;UTF-8&#39;,$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 != &#39;CP936&#39; && $encode != &#39;ASCII&#39; && $encode != &#39;GBK&#39; ? iconv($encode,&#39;GB18030&#39;,$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;
}
?>
Salin selepas log masuk
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan