php地方天气

<?php
header("Content-type: text/html; charset=utf-8");
//获取IP的函数
function GetIP(){
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$cip = $_SERVER["HTTP_CLIENT_IP"];
}
elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
$cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif(!empty($_SERVER["REMOTE_ADDR"])){
$cip = $_SERVER["REMOTE_ADDR"];
}
else{
$cip = "无法获取!";
}
return $cip;
}
$ip= GetIP();
$urllocation="http://api.map.baidu.com/location/ip?ak=TFyi03cqGpZAREZbhsboBTzE&ip=".$ip;
$location=json_decode(send_post($urllocation),true);
echo ($location["address"]);
//baidu ipapi http://developer.baidu.com/map/index.php?title=webapi

//发送post请求
$url = 'http://apis.baidu.com/heweather/weather/free?city=wenzhou';
function send_post($url){
$ch = curl_init();
$header = array(
'apikey: 7728db103ad7e801f1225d488ddce370',
);
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER , $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch , CURLOPT_URL , $url);
return curl_exec($ch);
}

//天气数据存在$weather变量中
$weahter=json_decode(send_post($url))->{"HeWeather data service 3.0"}[0];
echo ($weahter->{"basic"}->{"city"}."<br>");
echo ($weahter->{"now"}->{"cond"}->{"code"}."<br>");
echo ($weahter->{"now"}->{"cond"}->{"txt"}."<br>");
echo ($weahter->{"suggestion"}->{"comf"}->{"brf"}."<br>");
echo ($weahter->{"suggestion"}->{"comf"}->{"txt"}."<br>");
?>

posted @ 2015-11-24 22:02  Debugor  阅读(239)  评论(0编辑  收藏  举报