码迷,mamicode.com
首页 > Web开发 > 详细

php地方天气

时间:2015-11-24 22:56:16      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

<?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>");
?>

php地方天气

标签:

原文地址:http://www.cnblogs.com/wz0107/p/4993066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!