>php教程 >PHP源码 >一个高效的根据IP自动显示天气预报的方案

一个高效的根据IP自动显示天气预报的方案

PHP中文网
PHP中文网원래의
2016-05-25 17:05:371378검색

php代码

<?php
class weather{
	// 省和省会
	private $capital = array(
		&#39;黑龙江省&#39;=>&#39;哈尔滨市&#39;,
		&#39;吉林省&#39;=>&#39;长春市&#39;,
		&#39;辽宁省&#39;=>&#39;沈阳市&#39;,
		&#39;河北省&#39;=>&#39;石家庄市&#39;,
		&#39;山西省&#39;=>&#39;太原市&#39;,
		&#39;山东省&#39;=>&#39;济南市&#39;,
		&#39;青海省&#39;=>&#39;西宁市&#39;,
		&#39;甘肃省&#39;=>&#39;兰州市&#39;,
		&#39;陕西省&#39;=>&#39;西安市&#39;,
		&#39;河南省&#39;=>&#39;郑州市&#39;,
		&#39;江苏省&#39;=>&#39;南京市&#39;,
		&#39;四川省&#39;=>&#39;成都市&#39;,
		&#39;湖北省&#39;=>&#39;武汉市&#39;,
		&#39;安徽省&#39;=>&#39;合肥市&#39;,
		&#39;浙江省&#39;=>&#39;杭州市&#39;,
		&#39;湖南省&#39;=>&#39;长沙市&#39;,
		&#39;江西省&#39;=>&#39;南昌市&#39;,
		&#39;贵州省&#39;=>&#39;贵阳市&#39;,
		&#39;福建省&#39;=>&#39;福州市&#39;,
		&#39;台湾省&#39;=>&#39;台北市&#39;,
		&#39;云南省&#39;=>&#39;昆明市&#39;,
		&#39;广东省&#39;=>&#39;广州市&#39;,
		&#39;海南省&#39;=>&#39;海口市&#39;,
		&#39;上海市&#39;=>&#39;上海&#39;,
		&#39;北京市&#39;=>&#39;北京&#39;,
		&#39;天津市&#39;=>&#39;天津&#39;,
		&#39;重庆市&#39;=>&#39;重庆&#39;,
		&#39;香港&#39;=>&#39;香港&#39;,
		&#39;澳门&#39;=>&#39;澳门&#39;,
		&#39;新疆&#39;=>&#39;乌鲁木齐市&#39;,
		&#39;内蒙古&#39;=>&#39;呼和浩特市&#39;,
		&#39;宁夏&#39;=>&#39;银川市&#39;,
		&#39;西藏&#39;=>&#39;拉萨市&#39;,
		&#39;广西&#39;=>&#39;南宁市&#39;
	);
	public $province = &#39;北京&#39;;// 默认
	public $city = &#39;北京&#39;;// 默认

	// 获取ip
	static function getIP(){
		$ip = &#39;127.0.0.1&#39;;
		if(isset($_SERVER)){
			if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
				$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
			}else if(isset($_SERVER["HTTP_CLIENT_IP"])) {
				$ip = $_SERVER["HTTP_CLIENT_IP"];
			}else{
				$ip = $_SERVER["REMOTE_ADDR"];
			}
		}else{
			if(getenv("HTTP_X_FORWARDED_FOR")){
				$ip = getenv("HTTP_X_FORWARDED_FOR");
			}else if(getenv("HTTP_CLIENT_IP")) {
				$ip = getenv("HTTP_CLIENT_IP");
			}else{
				$ip = getenv("REMOTE_ADDR");
			}
		}
		return $ip;
	}

	public function setArea($addr){
		// 分割省市
		foreach($this->capital as $p => $c){
			if(strpos($addr,$p) === 0){
				$this->province = str_replace(array(&#39;省&#39;,&#39;市&#39;),&#39;&#39;,$p);
				$this->city = substr($addr,strlen($p));
				if($this->city == &#39;&#39;){
					$this->city = $c;// 默认省会
				}
			}
		}
	}
	public function getAreaCode(){
		$code = &#39;101010100&#39;;// 默认北京
		// 到本地地区库里找中国气象地区编码,可以把序列化数据直接反序列化后放到本文件里,会更快
		$area = file_get_contents("area.php");
		$area = unserialize($area);
		//print_r($area);
		$province = array();
		foreach($area as $k => $v){
			$province[$k] = $v[&#39;level0&#39;];
		}

		$pid = array_search($this->province,$province);
		if($pid !== false){
			$citys = $area[$pid][&#39;level1&#39;];
			$cityid = array_search($this->city,$citys);
			if($cityid !== false){
				$code = &#39;101&#39;.$cityid.&#39;01&#39;;
			}else{
				$code = &#39;101&#39;.$pid.&#39;0101&#39;;
			}
		}
		return $code;
	}
}

/* 
	纯真ip库返回的地区格式如下:
	xx省xx市 或者 xx市 如果是自治区则为:新疆乌鲁木齐市,分割省市极麻烦
*/

$weather = new weather();
// php的扩展iplocation
$addrarr = iplocation_fetch(weather::getIP());
$addr = iconv(&#39;gbk&#39;,&#39;utf-8&#39;,$addrarr[&#39;province&#39;]);
$weather->setArea($addr);
$code = $weather->getAreaCode();

// 尝试从redis里读取天气数据
$redis_host = &#39;127.0.0.1&#39;;
$redis_port = 6379;
/* redis connect */
$redis = new Redis();
$redis->connect($redis_host, $redis_port);

$data = $redis->get($code);
if(!$data){
	// 从中国气象去抓,并存入redis
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, "http://m.weather.com.cn/data/{$code}.html");
	curl_setopt($curl, CURLOPT_HEADER, 0);
	curl_setopt ($curl, CURLOPT_TIMEOUT, 30 );
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$data = curl_exec($curl);
	curl_close($curl);
	if($data){
		$redis->set($code,$data);
		$redis->EXPIRE($code,3600);
	}else{
		echo &#39;weather.com.cn 连接超时&#39;;
	}
}
print_r($data);
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.