-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
138 lines (113 loc) · 3.6 KB
/
index.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
//错误日志
function echo_server_log($log){
file_put_contents("log.txt", $log, FILE_APPEND);
}
//定义TOKEN
define ( "TOKEN", "ulink" );
//验证微信公众平台签名
function checkSignature() {
$signature = $_GET ['signature'];
$nonce = $_GET ['nonce'];
$timestamp = $_GET ['timestamp'];
$tmpArr = array ($nonce, $timestamp, TOKEN );
sort ( $tmpArr );
$tmpStr = implode ( $tmpArr );
$tmpStr = sha1 ( $tmpStr );
if ($tmpStr == $signature) {
return true;
}else{
return false;
}
}
if(false == checkSignature()) {
exit(0);
}
//接入时验证接口
$echostr = $_GET ['echostr'];
if($echostr) {
echo $echostr;
exit(0);
}
//获取POST数据
function getPostData() {
$data = $GLOBALS['HTTP_RAW_POST_DATA'];
return $data;
}
$PostData = getPostData();
//验错
if(!$PostData){
echo_server_log("wrong input! PostData is NULL");
echo "wrong input!";
exit(0);
}
//装入XML
$xmlObj = simplexml_load_string($PostData, 'SimpleXMLElement', LIBXML_NOCDATA);
//验错
if(!$xmlObj) {
echo_server_log("wrong input! xmlObj is NULL\n");
echo "wrong input!";
exit(0);
}
//准备XML
$fromUserName = $xmlObj->FromUserName;
$toUserName = $xmlObj->ToUserName;
$msgType = $xmlObj->MsgType;
if($msgType == 'voice') {//判断是否为语音
$content = $xmlObj->Recognition;
}elseif($msgType == 'text'){
$content = $xmlObj->Content;
}else{
$retMsg = '只支持文本和语音消息';
}
if (strstr($content, "温度")) {
$con = mysql_connect("127.0.0.1","root","root");
mysql_select_db("app_ulink42", $con);//修改数据库名
$result = mysql_query("SELECT * FROM sensor");
while($arr = mysql_fetch_array($result)){
if ($arr['ID'] == 1) {
$tempr = $arr['data'];
}
}
mysql_close($con);
$retMsg = "报告大王:"."\n"."主人房间的室温为".$tempr."℃,感谢您对主人的关心";
}else if (strstr($content, "开灯")) {
$con = mysql_connect("127.0.0.1","root","root");
$dati = date("h:i:sa");
mysql_select_db("app_ulink42", $con);//修改数据库名
$sql ="UPDATE switch SET timestamp='$dati',state = '1'
WHERE ID = '1'";//修改开关状态值
if(!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}else{
mysql_close($con);
$retMsg = "好的主人";
}
}else if (strstr($content, "关灯")) {
$con = mysql_connect("127.0.0.1","root","root");
$dati = date("h:i:sa");
mysql_select_db("app_ulink42", $con);//修改数据库名
$sql ="UPDATE switch SET timestamp='$dati',state = '0'
WHERE ID = '1'";//修改开关状态值
if(!mysql_query($sql,$con)){
die('Error: ' . mysql_error());
}else{
mysql_close($con);
$retMsg = "好的主人";
}
}else{
$retMsg = "暂时不支持该命令";
}
//装备XML
$retTmp = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$resultStr = sprintf($retTmp, $fromUserName, $toUserName, time(), $retMsg);
//反馈到微信服务器
echo $resultStr;
?>