-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetweather.php
More file actions
74 lines (73 loc) · 3.59 KB
/
Copy pathgetweather.php
File metadata and controls
74 lines (73 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
function weather($n, $postObj)
{
$weatherurl="http://api.map.baidu.com/telematics/v2/weather?location={$n}&ak=1a3cde429f38434f1811a75e1a90310c";
$apistr=file_get_contents($weatherurl);
$apiobj=simplexml_load_string($apistr);
$placeobj=$apiobj->currentCity;//当前城市
$season = date("m");
if($season == 3 || $season == 4 || $season == 5){
$seasonpic = "http://119.29.101.95/public/images/spring.gif";
}elseif($season == 6 || $season == 7 ||$season ==8){
$seasonpic = "http://119.29.101.95/public/images/summer.gif";
}elseif($season == 9 ||$season == 10 ||$season == 11){
$seasonpic = "http://119.29.101.95/public/images/autumn.gif";
}elseif($season == 12 || $season == 1 ||$season == 2){
$seasonpic = "http://119.29.101.95/public/images/winter.gif";
}
if(empty($placeobj))
{
$contentStr = "抱歉,没有查询到".$n."的天气信息";
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>1</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(), $contentStr);
}else{
$weatherobj1=$apiobj->results->result[0]->weather;//今天天气
$windobj1=$apiobj->results->result[0]->wind;//今天风力
$temobj1=$apiobj->results->result[0]->temperature;//今天气温
$picobj1=$apiobj->results->result[0]->dayPictureUrl;//今天图片
$weatherobj2=$apiobj->results->result[1]->weather;//明天天气
$windobj2=$apiobj->results->result[1]->wind;//明天风力
$temobj2=$apiobj->results->result[1]->temperature;//明天气温
$picobj2=$apiobj->results->result[1]->dayPictureUrl;//明天图片
$weatherobj3=$apiobj->results->result[2]->weather;//后天天气
$windobj3=$apiobj->results->result[2]->wind;//后天风力
$temobj3=$apiobj->results->result[2]->temperature;//后天气温
$picobj3=$apiobj->results->result[2]->dayPictureUrl;//后天图片
$weatherTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>4</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<PicUrl><![CDATA[%s]]></PicUrl>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<PicUrl><![CDATA[%s]]></PicUrl>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<PicUrl><![CDATA[%s]]></PicUrl>
</item>
<item>
<Title><![CDATA[%s]]></Title>
<PicUrl><![CDATA[%s]]></PicUrl>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml> ";
$resultStr = sprintf($weatherTpl, $postObj->FromUserName, $postObj->ToUserName, time(), $n."天气信息", $seasonpic, "今天 ".$temobj1."\n".$weatherobj1." ".$windobj1, $picobj1,"明天 ".$temobj2."\n".$weatherobj2." ".$windobj2, $picobj2,"后天 ".$temobj3."\n".$weatherobj3." ".$windobj3, $picobj3);
}
return $resultStr;
}
?>