-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.php
More file actions
338 lines (319 loc) · 14.2 KB
/
Copy pathtest.php
File metadata and controls
338 lines (319 loc) · 14.2 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "mijo");
define("APPID", "wx40bbdef91ec028d5");
define("APPSECRET", "d7f40569f91630363acf790d2f7c228d");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
//$wechatObj->valid();
class wechatCallbackapiTest
{
public function __construct()
{
require_once('Weather.php');
require_once('Reply.php');
require_once('Message.php');
require_once('User.php');
require_once('Music.php');
}
public function valid()
{
$echoStr = $_GET["echostr"];
file_put_contents("1.txt",$echoStr);
//valid signature , option
if ($this->checkSignature()) {
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = isset($GLOBALS["HTTP_RAW_POST_DATA"]) ? $GLOBALS["HTTP_RAW_POST_DATA"] : '';
//extract post data
if (!empty($postStr)) {
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE) {
case "text":
$resultStr = $this->handleText($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
case "image":
$resultStr = $this->handleImage($postObj);
break;
default:
$resultStr = "Unknow msg type: " . $RX_TYPE;
break;
}
echo $resultStr;
} else {
echo "error";
exit;
}
}
public function handleText($postObj)
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)) {
$keyword = trim($postObj->Content);
if (!empty($keyword)) {
$str_len = mb_strlen($keyword, "UTF-8");
$str_key = mb_substr($keyword, 2, $str_len - 1, "UTF-8");
$str = mb_substr($keyword, 0, 2, "UTF-8");
$str2 = mb_substr($keyword, 0, 3, "UTF-8");
$str_key2 = mb_substr($keyword, 3, $str_len - 1, "UTF-8");
if ($str == '天气' && !empty($str_key)) {
$weather = new Weather($postObj);
$weather->newLocation(trim($str_key), $postObj);
$resultStr = $weather->getWeather(trim($str_key), $postObj);
} elseif ($str2 == '添加@')//添加关键字回复
{
$reply = new Reply();
$result = $reply->updateReply($postObj, $keyword);
if ($result['action'] == "0") {
if ($result['result']) {
$contentStr = "修改成功!";
} else {
$contentStr = "修改失败!";
}
} else {
if ($result['result']) {
$contentStr = "插入成功!";
} else {
$contentStr = "插入失败!";
}
}
$resultStr = $this->responseText($postObj, $contentStr);
} elseif ($str2 == '报修#')//添加关键字回复
{
require('getrepaired.php');
$result = submitReport($postObj, $keyword);
$resultStr = $this->responseText($postObj, $result);
} elseif ($keyword == '随便听听')//随机播放音乐
{
$music = new Music();
$res = $music->randomPlay();
file_put_contents("5.txt",json_encode($res));
$media_id = $this->uploadTempImage($res['pic']);
$resultStr = $this->responseMusic($postObj, $res['title'], $res['author'], $res['url'], $media_id);
} elseif ($str == '播放' && !empty($str_key))//播放音乐
{
$music = new Music();
$res = $music->getMusic($str_key);
$media_id = $this->uploadTempImage($res['pic']);
$resultStr = $this->responseMusic($postObj, $res['title'], $res['author'], $res['url'], $media_id = '123456');
} elseif ($keyword == "天气") {
$weather = new Weather();
$result = $weather->getLocation($postObj);
if ($result == '0') {
$resultStr = $this->responseText($postObj, "请输入“天气地区“");
} else {
$resultStr = $weather->getWeather(trim($result), $postObj);
}
} elseif ($str2 == '漂流瓶' && !empty($str_key2))//投放漂流瓶
{
$contentStr = substr(strstr(trim($str_key2), '@'), 1);
$message = new Message();
$contentStr = $message->setMessage($postObj, $contentStr);
$resultStr = $this->responseText($postObj, $contentStr);
} elseif ($str == '我是' || $str == '我叫' && !empty($str_key))//音乐
{
$user = new User();
$contentStr = $user->newName($postObj, trim($str_key));
$resultStr = $this->responseText($postObj, $contentStr);
} else {
//关键字回复
if ($keyword == "你好" || $keyword == "hi" || $keyword == "Hi") {
$contentStr = "Hello~有什么能帮您的^_^";
$resultStr = $this->responseText($postObj, $contentStr);
} elseif ($keyword == "青石" || $keyword == "洲泉") {
$title = "洲泉往返青石";
$picurl = "http://tellmeapi-tellmestore.stor.sinaapp.com/bus/%E6%B4%B2%E6%B3%89%E5%88%B0%E9%9D%92%E7%9F%B3.jpg";
$url = "http://mp.weixin.qq.com/s?__biz=MzA4Mzk2ODkxNg==&mid=200599051&idx=1&sn=840341cbb9d19d95924881515f6dfe21#rd";
$contentStr = "洲泉新车站发车时间:06:00,07:20,08:50,10:30,12:20,14:10,16:00,17:10" . "\n" . "青石鞋业城发车时间:06:30,08:00,09:30,11:10,13:10,15:00,16:30";
$resultStr = $this->responseNews($postObj, $title, $picurl, $url, $contentStr);
} elseif ($keyword == "国际新闻") {
require_once('getnews.php');
$resultStr = getGlobalNews($postObj);
} elseif ($keyword == "国内新闻") {
require_once('getnews.php');
$resultStr = getHomeNews($postObj);
} elseif ($keyword == "新闻") {
require_once('getnews.php');
$resultStr = getNews($postObj);
} elseif ($keyword == "OSA" || $keyword == "osa") {
require_once('sign.php');
$contentStr = getUser();
$resultStr = $this->responseText($postObj, $contentStr);
} elseif ($keyword == "news" || $keyword == "News") {
require_once('getnews.php');
$resultStr = getEnglish($postObj);
} elseif ($keyword == "漂流瓶") {
$message = new Message();
$contentStr = $message->getMessage($postObj);
$resultStr = $this->responseText($postObj, $contentStr);
} else {
$reply = new Reply();
$contentStr = $reply->getReply($keyword);
if ($contentStr == "0") {
$contentStr = "您可以选择\n【1】试听音乐,回复“播放歌曲名”或“随便听听”,如“播放小苹果”;\n【2】查看天气情况,回复“天气城市”,如“天气杭州”。下次可直接回复“天气”即可查看上次地点天气;\n【3】查看新闻,回复“新闻”、“国内新闻”、“国际新闻”有不同惊喜;\n【4】漂流瓶,回复“漂流瓶”捡瓶子或“漂流瓶@你想说的话”,如“漂流瓶@我喜欢你!”来投放漂流瓶,会被别人捡到哦~";
$resultStr = $this->responseText($postObj, $contentStr);
} else {
$resultStr = $this->responseText($postObj, $contentStr);
}
}
}
return $resultStr;
} else {
return "Input something...";
}
} else {
return "error";
}
}
public function handleImage($postObj)
{
$picUrl = $postObj->MediaId;
$fromUserName = $postObj->ToUserName;
$toUserName = "olKHRt2bQZGVZIrsrXlk8DE5SZMg";
$imageTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
$resultStr = sprintf($imageTpl, $toUserName, $fromUserName, time(), $picUrl);
return $resultStr;
}
public function handleEvent($object)
{
switch ($object->Event) {
case "subscribe":
$contentStr = "感谢你的关注!你可以试试以下功能:" . "\n" . "【1】天气查询,回复 天气+城市 如 天气杭州;" . "\n" . "【2】歌曲播放,回复 播放+歌曲名+@歌手 如播放小苹果@筷子兄弟 或 播放小苹果;\n【3】秒速新闻,回复 新闻或国内新闻、国际新闻。";
break;
default:
$contentStr = "Unknow Event: " . $object->Event;
break;
}
$resultStr = $this->responseText($object, $contentStr);
return $resultStr;
}
public function responseText($object, $content, $flag = 0)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $resultStr;
}
public function responseNews($object, $title, $picurl, $url, $contentStr)
{
$picTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>1</ArticleCount>
<Articles>
<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
</Articles>
<FuncFlag>1</FuncFlag>
</xml> ";
$resultStr = sprintf($picTpl, $object->FromUserName, $object->ToUserName, time(), $title, $contentStr, $picurl, $url);
return $resultStr;
}
public function responseMusic($object, $title, $author, $url, $media_id)
{
$xml = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
<ThumbMediaId><![CDATA[%s]]></ThumbMediaId>
</Music>
</xml>";
$resultStr = sprintf($xml, $object->FromUserName, $object->ToUserName, time(), $title, $author, $url, $url, $media_id);
return $resultStr;
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function uploadTempImage($img)
{
file_put_contents("1.jpg",file_get_contents($img));
$access_token = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type=image";
$file = array('media'=>'@'.realpath("1.jpg"));
$data = json_decode($this->curl_post($url, $file));
return $data->media_id;
}
public function getAccessToken()
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
$data = json_decode(file_get_contents($url),true);
if($data['access_token']){
return $data['access_token'];
}else{
echo "Error";
exit();
}
}
//curl实现post请求
function curl_post($url, $data)
{
//创建一个新cURL资源
$curl = curl_init();
//设置URL和相应的选项
curl_setopt($curl, CURLOPT_URL, $url);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//执行curl,抓取URL并把它传递给浏览器
$output = curl_exec($curl);
//关闭cURL资源,并且释放系统资源
curl_close($curl);
var_dump($output);
return $output;
}
}