PHP天气API接口.docx
《PHP天气API接口.docx》由会员分享,可在线阅读,更多相关《PHP天气API接口.docx(38页珍藏版)》请在冰豆网上搜索。
![PHP天气API接口.docx](https://file1.bdocx.com/fileroot1/2022-10/29/bb046f5b-678e-4091-a815-7fddfa99d584/bb046f5b-678e-4091-a815-7fddfa99d5841.gif)
PHP天气API接口
PHP天气API接口
[导读]中国天气网的API还每次还要先去判断ID,再获取天气信息,觉得有点麻烦,所以自己做了一个PHP一部搞定的。
还有一个农历API。
phpheader(&39;Access-Control-Allow-Origin:
*&39;);header("Content-Type:
text
中国天气网的API还每次还要先去判断ID,再获取天气信息,觉得有点麻烦,所以自己做了一个PHP一部搞定的。
还有一个农历API。
viewsourceprint?
01.
php
02.
header('Access-Control-Allow-Origin:
*');
03.
header("Content-Type:
text/javascript;charset=utf-8");
04.
/**
05.
*获取用户真实IP
06.
*/
07.
functiongetIP()
08.
{
09.
static$realip;
10.
if(isset($_SERVER)){
11.
if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
12.
$realip=$_SERVER["HTTP_X_FORWARDED_FOR"];
13.
}elseif(isset($_SERVER["HTTP_CLIENT_IP"])){
14.
$realip=$_SERVER["HTTP_CLIENT_IP"];
15.
}else{
16.
$realip=$_SERVER["REMOTE_ADDR"];
17.
}
18.
}else{
19.
if(getenv("HTTP_X_FORWARDED_FOR")){
20.
$realip=getenv("HTTP_X_FORWARDED_FOR");
21.
}elseif(getenv("HTTP_CLIENT_IP")){
22.
$realip=getenv("HTTP_CLIENT_IP");
23.
}else{
24.
$realip=getenv("REMOTE_ADDR");
25.
}
26.
}
27.
28.
29.
return$realip;
30.
}
31.
32.
33.
/**
34.
*获取IP地理位置
35.
*淘宝IP接口
36.
*@Return:
array
37.
*/
38.
functiongetCity($ip)
39.
{
40.
$url="
41.
$ip=file_get_contents($url));
42.
if((string)$ip->code=='1'){
43.
returnfalse;
44.
}
45.
$data=$ip->data->city;
46.
return$data;
47.
}
48.
49.
functiongetWeather($cityId)
50.
{
51.
$url="
52.
$weather=file_get_contents($url));
53.
return$weather;
54.
}
55.
function$web){
56.
$arr=array();
57.
foreach($webas$k=>$w){
58.
if(is_object($w))$arr[$k]=$w);//判断类型是不是object
59.
else$arr[$k]=$w;
60.
}
61.
return$arr;
62.
}
63.
$city=getCity(getIP());
64.
$city=str_split($city,strlen($city)-3);
65.
$city=$city[0];
66.
$cityUrl="
67.
$web=file_get_contents($cityUrl));
68.
$arr=$web);
69.
$weatherInfo=getWeather($arr[$city]);
70.
echo('weather(');
71.
echo($weatherInfo));
72.
echo(")");
73.
74.
?
>
城市源码
viewsourceprint?
01.
DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http:
//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
02.
//www.w3.org/1999/xhtml">
03.
04.
05.
无标题文档06.
07.
08.
09.
10.
11.
12.
13.
17.
18.
19.
20.
21.
22.
23.
26.
27.
28.
29.
30.
31.
32.
35.
36.