-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweixin.class.php
More file actions
383 lines (330 loc) · 10 KB
/
weixin.class.php
File metadata and controls
383 lines (330 loc) · 10 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
<?php
require 'weixin.config.php';
class wxmessage
{
const MSG_TYPE_TEXT = 'text';
const MSG_TYPE_IMAGE='image';
const MSG_TYPE_LINK='link';
const MSG_TYPE_LOCATION = 'location';
const MSG_TYPE_EVENT='event';//Event Push only supports Wechat 4.5 or above. Will be coming soon
const REPLY_TYPE_MUSIC='music';
const REPLY_TYPE_TEXT = 'text';
const REPLY_TYPE_NEWS = 'news';
private $_postData;
private $_token;
public function __construct()
{
if (! defined('TOKEN'))
throw new Exception('Token is required');
if (method_exists($this, 'errorHandler'))
set_error_handler(array($this, 'errorHandler'));
if (method_exists($this, 'exceptionHandler'))
set_exception_handler(array($this, 'exceptionHandler'));
$this->_token = TOKEN;
$this->parsePostRequestData();
}
public function run()
{
if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
if ($this->_postData && $this->beforeProcess($this->_postData) === true) {
$this->processRequest($this->_postData);
$this->afterProcess();
}
else
throw new Exception('POST data is wrong or beforeProcess method doesn\'t return true');
}
else
$this->sourceCheck();
exit(0);
}
/**
* check text msg
* @return boolean
*/
public function isTextMsg()
{
return $this->_postData->MsgType == self::MSG_TYPE_TEXT;
}
/**
* check location
* @return boolean
*/
public function isLocationMsg()
{
return $this->_postData->MsgType == self::MSG_TYPE_LOCATION;
}
/**
* check image
* @return boolean
*/
public function isImageMsg(){
return $this->_postData->MsgType == self::MSG_TYPE_IMAGE;
}
/**
* check links
* @return boolean
*/
public function isLinkMsg(){
return $this->_postData->MsgType == self::MSG_TYPE_LINK;
}
/**
* check event push
* @return boolean
*/
public function isEventMsg(){
return $this->_postData->MsgType == self::MSG_TYPE_EVENT;
}
/**
* generate text msg string
* @param string $content
* @return string xml
*/
public function outputText($content)
{
$textTpl = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>';
$text = sprintf($textTpl, $this->_postData->FromUserName, $this->_postData->ToUserName, time(), self::REPLY_TYPE_TEXT, $content);
return $text;
}
/**
* generate text & images msg string
* @param string $content
* @param arrry $posts article array. Every item is an article array, the keys are in consistent of the official instructions.
* @return string xml
*/
public function outputNews($posts = array())
{
$textTpl = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>%d</ArticleCount>
<Articles>%s</Articles>
<FuncFlag>1<FuncFlag>
</xml>';
$itemTpl = '<item>
<Title><![CDATA[%s]]></Title>
<Discription><![CDATA[%s]]></Discription>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>';
$items = '';
foreach ((array)$posts as $p) {
if (is_array($p))
$items .= sprintf($itemTpl, $p['title'], $p['discription'], $p['picurl'], $p['url']);
else
throw new Exception('$posts data structure wrong');
}
$text = sprintf($textTpl, $this->_postData->FromUserName, $this->_postData->ToUserName, time(), self::REPLY_TYPE_NEWS, count($posts), $items);
return $text;
}
public function outputMusic($musicpost){
$textTpl = '<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Music>%s</Music>
<FuncFlag>0</FuncFlag>
</xml>';
$musicTpl = '
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
';
$music = '';
if (is_array($musicpost)){
$music .= sprintf($musicTpl, $musicpost['title'], $musicpost['discription'], $musicpost['musicurl'], $musicpost['hdmusicurl']);
}else{
throw new Exception('$posts data structure wrong');
}
$text = sprintf($textTpl, $this->_postData->FromUserName, $this->_postData->ToUserName, time(), self::REPLY_TYPE_MUSIC, $music);
return $text;
}
/**
* Prase the received post arra
* @return SimpleXMLElement
*/
public function parsePostRequestData()
{
$rawData = $GLOBALS['HTTP_RAW_POST_DATA'];
$data = simplexml_load_string($rawData, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($data !== false)
$this->_postData = $data;
return $data;
}
/**
* return the received post array
* @return object
*/
public function getPostData()
{
return $this->_postData;
}
protected function beforeProcess($postData)
{
return true;
}
protected function afterProcess()
{
}
protected function processRequest($data)
{
throw new Exception('This method must be rewrite');
}
/**
* check url source is correct
* @return boolean
*/
private function checkSignature()
{
$signature = $_GET['signature'];
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$params = array($this->_token, $timestamp, $nonce);
sort($params);
$sig = sha1(implode($params));
return $sig == $signature;
}
private function sourceCheck()
{
if ($this->checkSignature()) {
$echostr = $_GET['echostr'];
echo $echostr;
}else{
throw new Exception('Wrong Signature');
}
exit(0);
}
/**
*获取用户access_token
*@param type $code 授权时获得code值
*@return type
*/
public static function getAuthToken($code) {
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.APPID.'&secret='.APPSECRET.'&code='.$code.'&grant_type=authorization_code';
$content = curl_get($url);
$ret = json_decode($content, true);
return $ret;
}
}
/**
* POST data
*/
function curl_post($url,$data){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);
}
curl_close($curl);
return $tmpInfo;
}
/**
* GET data
*/
function curl_get($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if(!curl_exec($ch)){
error_log(curl_error($ch));
$data='';
}else{
$data = curl_multi_getcontent($ch);
}
curl_close($ch);
return $data;
}
/**
*WeChat general interface
*/
class wxcommon{
/**
* get the Token��
*@return {"access_token":"ACCESS_TOKEN","expires_in":7200} or false
*/
const API_URL = 'http://api.weixin.qq.com';
public static function getToken(){
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".APPSECRET;
$content=file_get_contents($url);
$ret=json_decode($content,true);
if(array_key_exists('errcode',$ret)){
return false;
}else{
return $ret;
}
}
}
/**
*WeChat Custom Menu
*/
class wxmenu{
private $_ACCESS_TOKEN;
public function __construct($accesstoken)
{
$this->_ACCESS_TOKEN=$accesstoken;
}
/**
*create the menu
*@return true or false
*/
public function createMenu($menu){
$url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->_ACCESS_TOKEN;
$content=curl_post($url,$menu);
$ret=json_decode($content,true);
if($ret['errcode']==0){
return true;
}else{
return false;
}
}
/**
*get the menu
*@return menu in json,or false
*/
public function getMenu(){
$url="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$this->_ACCESS_TOKEN;
$content=file_get_contents($url);
if(strpos($content, 'errcode') === false){
return $content;
}else{
return false;
}
}
/**
*delete the menu
*@return true or false
*/
public function deleteMenu(){
$url="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=".$this->_ACCESS_TOKEN;
$content=file_get_contents($url);
$ret=json_decode($content,true);
if($ret['errcode']==0){
return true;
}else{
return false;
}
}
}