![]() |
|
首页 │ Apache │ Linux│ Java│ MySQL│ 注册│帮助 | |||
相关问题
数据取自QQ天气预报。
带缓存功能,每天只取一次数据。速度很快。
CODE: <?php /* +---------------------------------------------------+ | Name : NeatQQWeatherClass (QQ天气预报小偷类) +---------------------------------------------------+ | Created / Modify : 2003-10-10 / 2004-10-11 +---------------------------------------------------+ | Version : 1.1.0 +---------------------------------------------------+ | Author : walkerlee +---------------------------------------------------+ | Powered by NEATSTUDIO 2002 - 2004 +---------------------------------------------------+ | Email : [email]neatstudio@yahoo.com.cn[/email] | Homepge : [url]http://www.neatstudio.com[/url] | BBS : [url]http://www.neatstudio.com/bbs/[/url] +---------------------------------------------------+ | Note : | | 1.本软件对于非商业用户完全免费,如果要使用在商业用途 | 方面,必须取得作者的授权. | | 2.你可以任意传播以及修改本程序,但不能以任何形式删除 | 本程序的版权.请记住,保留作者版权是对作者工作的尊敬. | | 3.如果有问题,可以通过上面提供的方式进行解答,但作者 | | 如果不能及时或者不解答,请谅解. | | 4.作者对使用该程序导致的问题,不予以负责. | | 5.本程序版权归 NeatStudio 所有.禁止任何侵权行为! | +---------------------------------------------------+ | 支持城市 : +---------------------------------------------------+ | 北京 | 上海 | 天津 | 石家庄 | 哈尔滨 | 沈阳 | 长春 | 太原 | 济南 | 郑州 | 呼和浩特 | 西安 | 银川 | 兰州 | 西宁 | 乌鲁木齐 | 合肥 | 南昌 | 南京 | 杭州 | 武汉 | 长沙 | 广州 | 深圳 | 福州 | 厦门 | 南宁 | 桂林 | 海口 | 重庆 | 成都 | 贵阳 | 昆明 | 拉萨 | 香港 | 澳门 | 台北 +---------------------------------------------------+ */ class NeatQQWeather { /* +----------------------+ | 配置 +----------------------+ */ var $url; var $contents = ""; function NeatQQWeather($city_name) { $this->url = 'http://appnews.qq.com/cgi-bin/news_weather_search?city=' . $city_name; $this->cache(); } /* +----------------------+ | 取页面 +----------------------+ */ function getPage() { $this->contents = file_get_contents($this->url); } /* +----------------------+ | 取数据 +----------------------+ */ function getData() { preg_match_all("/<td width=\"20\" class=\"text02\"> <font color=\"#FF0000\">●<\/font><\/td>\r\n <td class=\"text02\">\r\n(.*?)\r\n <\/td>\r\n <\/tr>\r\n <\/table><\/td>/is", $this->contents, $this->tcity); preg_match_all("/<div align=\"center\"><font color=\"#FFFFFF\">(.*?)<\/font><\/div>/is", $this->contents, $this->tdate); preg_match_all("/<img src=\"\/images\/weather_p(.*?).gif\" width=\"23\" height=\"23\">(.*?)<\/div>/is", $this->contents, $this->tweather); preg_match_all("/<div align=\"center\">风向风力<\/div><\/td>\r\n <td bgcolor=\"#FFFFFF\" class=\"text02\"> <div align=\"center\">(.*?)<\/div>/is", $this->contents, $this->twind); preg_match_all("/最低气温\(℃\)<\/div><\/td>\r\n <td height=\"22\" bgcolor=\"#FFFFFF\" class=\"text02\"><div align=\"center\">(.*?)<\/div><\/td>/is", $this->contents, $this->tlow); preg_match_all("/最高气温\(℃\)<\/div><\/td>\r\n <td bgcolor=\"#FFFFFF\" class=\"text02\"><div align=\"center\">(.*?)<\/div><\/td>/is", $this->contents, $this->thight); } function fetchData() { return $this->data; } /* +----------------------+ | 整理数据 +----------------------+ */ function makeArray() { $this->data['city'] = trim($this->tcity[1][0]); $this->data['stat']['today']['date'] = $this->tdate[1][0]; $this->data['stat']['tomorrow']['date'] = $this->tdate[1][1]; $this->data['stat']['today']['img'] = "<img src=\"http://appnews.qq.com/images/weather_p" . $this->tweather[1][0] . ".gif\" width=\"23\" height=\"23\">"; $this->data['stat']['tomorrow']['img'] = "<img src=\"http://appnews.qq.com/images/weather_p" . $this->tweather[1][1] . ".gif\" width=\"23\" height=\"23\">"; $this->data['stat']['today']['img_url'] = "http://appnews.qq.com/images/weather_p" . $this->tweather[1][0] . ".gif"; $this->data['stat']['tomorrow']['img_url'] = "http://appnews.qq.com/images/weather_p" . $this->tweather[1][1] . ".gif"; $this->data['stat']['today']['weather'] = $this->tweather[2][0]; $this->data['stat']['tomorrow']['weather'] = $this->tweather[2][1]; $this->data['stat']['today']['wind'] = $this->twind[1][0]; $this->data['stat']['tomorrow']['wind'] = $this->twind[1][1]; $this->data['stat']['today']['low'] = $this->tlow[1][0]; $this->data['stat']['tomorrow']['low'] = $this->tlow[1][1]; $this->data['stat']['today']['hight'] = $this->thight[1][0]; $this->data['stat']['tomorrow']['hight'] = $this->thight[1][1]; } /* +----------------------+ | 调试模式 +----------------------+ */ function debug() { echo "<pre>"; print_r($this->data); echo "</pre>"; } /* +----------------------+ | 缓存数据 +----------------------+ | 2004-10-11 +----------------------+ */ function cache() { if(file_exists("./NQWCache.php")) include_once("./NQWCache.php"); $this->cacheToFile(); } function cacheToFile() { if (empty($this->cacheCreateDate) || (date("YmdHis") - $this->cacheCreateDate > 1000000)) { $this->getPage(); $this->getData(); $this->makeArray(); if (is_writeable('./')) { $cacheFile .= "<?php\r\n\r\n"; $cacheFile .= "/*\r\n"; $cacheFile .= "+----------------------------------+\r\n"; $cacheFile .= "| Powered By NeatQQWeatherClass\r\n"; $cacheFile .= "+----------------------------------+\r\n"; $cacheFile .= "| Cache file\r\n"; $cacheFile .= "+----------------------------------+\r\n"; $cacheFile .= "*/\r\n\r\n"; $cacheFile .= "\$this->cacheCreateDate = " . date("YmdHis") . ";\r\n\r\n"; $cacheFile .= "\$this->data = " . $this->arrayeval($this->data); $cacheFile .= "?>"; $fp = fopen("./NQWCache.php", "w+"); fwrite($fp, $cacheFile); fclose($fp); header("location:".$_SERVER['PHP_SELF']); } } } function arrayeval($array, $level = 0) { for($i = 0; $i <= $level; $i++) { $space .= "\t"; } $evaluate = "Array\n$space(\n"; $comma = "$space"; foreach($array as $key => $val) { $key = is_string($key) ? "'".addcslashes($key, '\'\\')."'" : $key; $val = is_string($val) ? "'".addcslashes($val, '\'\\')."'" : $val; if(is_array($val)) { $evaluate .= "$comma$key => ".$this->arrayeval($val, $level + 1); } else { $evaluate .= "$comma$key => $val"; } $comma = ",\n$space"; } $evaluate .= "\n$space)"; return $evaluate; } } /* $NQW = new NeatQQWeather('南宁'); $NQW->debug(); */ ?> |
提问者:walker 08-15 09:09
答复

