class weather{
protected $url='http://php.weather.sina.com.cn/xml.php';
//http://php.weather.sina.com.cn/xml.php?city=%C4%CF%B2%FD&password=DJOYnieT8234jlsK&day=0(city gb2312urlencode编码 day查询日,0为当天1为明天)
//根据ip获取城市地址
protected function GetIp()
{
$realip = '';
$unknown = 'unknown';
if (isset($_SERVER)){
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach($arr as $ip){
$ip = trim($ip);
if ($ip != 'unknown'){
$realip = $ip;
break;
}
}
}else if(isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)){
$realip = $_SERVER['HTTP_CLIENT_IP'];
}else if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)){
$realip = $_SERVER['REMOTE_ADDR'];
}else{
$realip = $unknown;
}
}else{
if(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)){
$realip = getenv("HTTP_X_FORWARDED_FOR");
}else if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)){
$realip = getenv("HTTP_CLIENT_IP");
}else if(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)){
$realip = getenv("REMOTE_ADDR");
}else{
$realip = $unknown;
}
}
$realip = preg_match("/[\d\.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;
return $realip;
}
protected function GetIpLookup($ip = '')
{
if(empty($ip)){
$ip = GetIp();
}
$res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
if(empty($res)){ return false; }
$jsonMatches = array();
preg_match('#\{.+?\}#', $res, $jsonMatches);
if(!isset($jsonMatches[0])){ return false; }
$json = json_decode($jsonMatches[0], true);
if(isset($json['ret']) && $json['ret'] == 1){
$json['ip'] = $ip;
unset($json['ret']);
}else{
return false;
}
return $json;
}
//获取天气
protected function GetWeather($city,$day)
{
$citycode=urlencode(mb_convert_encoding($city,'gb2312','utf-8' ));
$url=$this->url.'?city='.$citycode.'&password=DJOYnieT8234jlsK&day='.$day;
$res = @file_get_contents($url);
$xmlstring = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring),true);
return $val;
}
public function OpenApiGetWeather()
{
$ip=$this->GetIp();
$address=$this->GetIpLookup($ip);
if(empty($address)){
$city='深圳';
}else{
$city=$address['city'];
}
$res=$this->GetWeather($city,0);
return $res;
}
}
//
$object=new weather();
$resc=$object->OpenApiGetWeather();
protected $url='http://php.weather.sina.com.cn/xml.php';
//http://php.weather.sina.com.cn/xml.php?city=%C4%CF%B2%FD&password=DJOYnieT8234jlsK&day=0(city gb2312urlencode编码 day查询日,0为当天1为明天)
//根据ip获取城市地址
protected function GetIp()
{
$realip = '';
$unknown = 'unknown';
if (isset($_SERVER)){
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach($arr as $ip){
$ip = trim($ip);
if ($ip != 'unknown'){
$realip = $ip;
break;
}
}
}else if(isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)){
$realip = $_SERVER['HTTP_CLIENT_IP'];
}else if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)){
$realip = $_SERVER['REMOTE_ADDR'];
}else{
$realip = $unknown;
}
}else{
if(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)){
$realip = getenv("HTTP_X_FORWARDED_FOR");
}else if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)){
$realip = getenv("HTTP_CLIENT_IP");
}else if(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)){
$realip = getenv("REMOTE_ADDR");
}else{
$realip = $unknown;
}
}
$realip = preg_match("/[\d\.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;
return $realip;
}
protected function GetIpLookup($ip = '')
{
if(empty($ip)){
$ip = GetIp();
}
$res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
if(empty($res)){ return false; }
$jsonMatches = array();
preg_match('#\{.+?\}#', $res, $jsonMatches);
if(!isset($jsonMatches[0])){ return false; }
$json = json_decode($jsonMatches[0], true);
if(isset($json['ret']) && $json['ret'] == 1){
$json['ip'] = $ip;
unset($json['ret']);
}else{
return false;
}
return $json;
}
//获取天气
protected function GetWeather($city,$day)
{
$citycode=urlencode(mb_convert_encoding($city,'gb2312','utf-8' ));
$url=$this->url.'?city='.$citycode.'&password=DJOYnieT8234jlsK&day='.$day;
$res = @file_get_contents($url);
$xmlstring = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring),true);
return $val;
}
public function OpenApiGetWeather()
{
$ip=$this->GetIp();
$address=$this->GetIpLookup($ip);
if(empty($address)){
$city='深圳';
}else{
$city=$address['city'];
}
$res=$this->GetWeather($city,0);
return $res;
}
}
//
$object=new weather();
$resc=$object->OpenApiGetWeather();
print_r($resc);exit;
结果:
Array | |
( | |
[Weather] => Array | |
( | |
[city] => 深圳 | |
[status1] => 阴 | |
[status2] => 阴 | |
[figure1] => yin | |
[figure2] => yin | |
[direction1] => 无持续风向 | |
[direction2] => 无持续风向 | |
[power1] => ≤3 | |
[power2] => ≤3 | |
[temperature1] => 23 | |
[temperature2] => 18 | |
[ssd] => 6 | |
[tgd1] => 21 | |
[tgd2] => 21 | |
[zwx] => 2 | |
[ktk] => 4 | |
[pollution] => 3 | |
[xcz] => 4 | |
[zho] => Array | |
( | |
) | |
[diy] => Array | |
( | |
) | |
[fas] => Array | |
( | |
) | |
[chy] => 3 | |
[zho_shuoming] => 暂无 | |
[diy_shuoming] => 暂无 | |
[fas_shuoming] => 暂无 | |
[chy_shuoming] => 单层薄衫、裤薄型棉衫、长裤、针织长袖衫、长袖T恤。薄型套装、牛仔衫裤、西服套装、薄型夹克 | |
[pollution_l] => 轻度 | |
[zwx_l] => 弱 | |
[ssd_l] => 温暖舒适 | |
[fas_l] => 暂无 | |
[zho_l] => 暂无 | |
[chy_l] => 单衣类 | |
[ktk_l] => 不需要开启 | |
[xcz_l] => 不太适宜 | |
[diy_l] => 暂无 | |
[pollution_s] => 对空气污染物扩散无明显影响 | |
[zwx_s] => 紫外线弱 | |
[ssd_s] => 天气状况良好时,多到户外活动,并可适当增加户外活动时间。 | |
[ktk_s] => 不需要开启空调 | |
[xcz_s] => 洗车后未来1-2天内有降水、大风或沙尘天气,不太适宜洗车 | |
[gm] => 1 | |
[gm_l] => 低发期 | |
[gm_s] => 天气舒适,不易发生感冒; | |
[yd] => 2 | |
[yd_l] => 比较适宜 | |
[yd_s] => 虽然天空云层较厚,比较适宜户外运动; | |
[savedate_weather] => 2017-04-13 | |
[savedate_life] => 2017-04-13 | |
[savedate_zhishu] => 2017-04-13 | |
[udatetime] => 2017-04-13 08:10:00 | |
) | |
) |