天气预报集成函数(一)

2014-11-24 03:26:53 · 作者: · 浏览: 0
/**
 * Weather
 *
 * @package		Weather
 * @author		王长宏
 * @link		http://blog.csdn.net/wang350
 */

// ------------------------------------------------------------------------

/**
 * 
 * 调用方法
 * $weather = new Weather;
 * //这个日期只能是今天和未来5天内的时间,否则返回false
 * $weather->get('2013-12-13');//默认是今天
 * 如果是今天的话,则得到的是实时天气情况;如果是未来的,则是气温范围
 * 需要其他的天气情况的话,自己按需要整理 getWeather() 函数
 * 接口详细信息 请参考 blog :http://blog.mynook.info/2012/08/18/weather-com-cn-api.html
 * 
 * 注意:
 * 本函数只能得到当天的 PM2.5 API文档:http://www.juhe.cn/docs/api/id/33/aid/76
 * 
 * 如果得到未来的请参考(需要付费) http://www.thinkpage.cn/weather/api/#getWeather 这个可以得到未来的空气质量指数
 * 
 * 注意,如果接口服务器不稳定,如果没有得到空气质量的数据,则不会显示。
 * 
 * 本文档一些函数来自网络,感谢前辈们的付出,谢谢!
 */

class Weather
{
	var $cache = 1;
	var $cache_time = 3600;
	
	var $city = '';
	//城市的拼音
	var $citypy = '';
	var $citycode = '';
	var $juheAppKey = '74******************ca';
	
	function __construct(){
		$this->setCity();
	}
	
	function getcache(){
		$weather_name = 'weather'.$this->date;
		if( !$this->cache || !isset($_COOKIE[$weather_name]) ||  !$_COOKIE[$weather_name]){
			$weather = $this->getWeather();
			setcookie($weather_name, json_encode($weather), time() + $this->cache_time, '/');
		}else{
			$weather = $this->json_to_array( json_decode($_COOKIE[$weather_name]) );
		}
		
		return $weather;
	}
	
	
	function get($date=''){
		if(!$date){
			$date = strtotime( date('Y-m-d') );
		}else{
			$date = strtotime($date);
		}
		
		$this->date = $date;
		
		return $this->getcache();
	}
	
	function _getAir(){
		$city = iconv('UTF-8', 'GB2312', $this->
city); $url = 'http://web.juhe.cn:8080/environment/air/pm city='.$this->city.'&key='.$this->juheAppKey; //create cURL connection $curl_connection = curl_init($url); //set options //curl_setopt($curl_connection, CURLOPT_COOKIE, $cookie); curl_setopt($curl_connection, CURLOPT_COOKIESESSION, true); curl_setopt($curl_connection, CURLOPT_POST, false); curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curl_connection, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); /* * set data to be posted, * Note: Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, * while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded. */ //perform our request $result = curl_exec($curl_connection); //show information regarding the request //print_r(curl_getinfo($curl_connection)); //echo curl_errno($curl_connection) . '-' . curl_error($curl_connection); //close the connection curl_close($curl_connection); if( $result ){ $airInfo = json_decode($result); return $this->json_to_array($airInfo->result[0]); } return array(); } function setCity(){ if(!$this->city){ if( !isset($_COOKIE['user_ip']) || !$_COOKIE['user_ip']){ $ip = $this->getIP(); if($ip == '127.0.0.1'){ $ip = '222.128.107.66'; } setcookie('user_ip', $ip, time() + 99999999999, '/'); }else{ $ip = $_COOKIE['user_ip']; }