PHP obtains the weather forecast for the current day and 72 hours, and generates the interface-PHP source code.

Source: Internet
Author: User
PHP obtains the weather forecast for the current day and 72 hours, and generates interface parameters:

City, city ID. for details, see the following table. for example, 212 is Chongqing.

Type, output format, json or xml. the default value is json.

Charset, output character encoding, UTF-8, utf8, or gbk. the default value is UTF-8.

Datetype, date output type, unix or other. the default value is unix timestamp.

For example, getweather. php? Type = xml> city = 212

Principle:
Read the HTML code of http://weather.news.qq.com/inc/07_dc'. $ city. '.htm and retrieve the result using regular expressions.

City list:

1. Hong Kong

2 Macao

17 Harbin

28 Urumqi

56 Xining

57 Lanzhou

69 Hohhot

78 Yinchuan

82 Shijiazhuang

84 Taiyuan

103 Changchun

115 Shenyang

125 Beijing

Tianjin 127

140 Jinan

150 Lhasa

166 Chengdu

179 Kunming

186 Xi'an

189 Zhengzhou

211 Wuhan

Chongqing, 212

218 Changsha

227 Guiyang

232 Guilin

244 Nanjing

248 Hefei

Shanghai 252

255 Hangzhou

264 Nanchang

276 Fuzhou

280 Taipei

287 Xiamen

Guangzhou 292

295 Nanning

296 Shenzhen

303 Haikou

Getweather. php

 (.[^\&]*)\ (.[^<]*)<\/strong>/i',$content,$a);    $weather['area'] = trim($a[2]);    $weather['now']['date'] = parseDate($a[1]);         preg_match('/(.[^<]*)
(.[^<]*)<\/td>/i',$content,$a); $weather['now']['weather'] = trim($a[1]); $weather['now']['temp'] = trim($a[2]); preg_match('/class="explain\sblues">(.[^<]*)
\s*(.[^<]*)
\s*(.[^<]*)(.[^<]*)<\/td>(.[^-]*)bgcolor="#EEF3F8">(.[^<]*)
(.[^<]*)
(.[^<]*)/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>:(.[^<]*)<\/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 '
';            print_r($weather);            echo '
'; 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; }} ?>

Encode. func. php

 $ Val) {$ string [$ key] = any2utf8 ($ val); // recursion} else {$ string = anystring2utf8 ($ string);} return $ string ;} /*** convert any encoded string (array) to gbk *** @ param mixed $ string input string (array) * @ return mixed output gbk encoded string (array) */function any2gbk ($ string) {// uses Recursive conversion to encode the string if (is_array ($ string) {foreach ($ string as $ key => $ val) {$ string [$ key] = any2gbk ($ val); // recursive} else {$ string = anystring2gbk ($ string);} return $ string ;}? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.