-
Notifications
You must be signed in to change notification settings - Fork 0
/
weichat.class.php
640 lines (597 loc) · 21.9 KB
/
weichat.class.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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
<?php
class WeiChat {
private $_appid;
private $_appsecret;
private $_token; //不是access_tokens
public function __construct($appid,$appsecret,$token) {
$this->_appid = $appid;
$this->_appsecret = $appsecret;
$this->_token = $token;
}
/**
* 获取AccessToken
* @return string
*/
public function getAccesstoken() {
if( $access_token = $this->getCacheValue('access_token')) {
return $access_token;
}
//如果token文件不存在或过期,则再次请求
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->_appid&secret=$this->_appsecret";
$content = curl_request($url);
$acc_obj = json_decode($content);
$this->setCacheValue('access_token', $acc_obj->access_token, $acc_obj->expires_in);
return $acc_obj->access_token;
}
/**
* 获取缓存中的值
* @param string $key
* @return mixed 错误时返回false
*/
private function getCacheValue($key) {
if( defined('RUN_ON_SAE') && RUN_ON_SAE) {
$mem = new Memcached();
$content = $mem->get($key);
return $content;
} elseif( defined('DRUPAL_ROOT')) { //for drupal
$expire = variable_get('tri_wechat_'.$key.'_expire');
if(isset($expire)) {
if( time()< $expire) {
return variable_get('tri_wechat_'.$key);
}
}
return false;
} else {
//如果token文件存在,则直接读文件
$file = './'.$key;
if( file_exists($file)) {
$content = file_get_contents($file);
$content = json_decode($content, true);
if( time()-filemtime($file)< $content['expires_in']) { /*filemtime文件编辑时间*/
return $content[$key];
}
}
return false;
}
}
/**
* 设置缓存中的值
* @param string $key
* @return void
*/
private function setCacheValue($key, $value, $expires_in) {
if(defined('RUN_ON_SAE') && RUN_ON_SAE) {
$mem = new Memcached();
return $mem->set($key, $value, $expires_in);
} elseif( defined('DRUPAL_ROOT')) { //for drupal
variable_set('tri_wechat_'.$key, $value);
variable_set('tri_wechat_'.$key.'_expire', time()+$expires_in);
return true;
} else {
$file = './'.$key;
$js_obj = array(
$key => $value,
'expires_in'=> $expires_in,
);
return file_put_contents($file, json_encode($js_obj));
}
}
/**
* 获取二维码Ticket
* @param int $expire_seconds 有效期
* @param string $type 二维码类型
* @param int $scene_id 场景编号Id
* @return string
*/
private function getTicket($expire_seconds, $type, $scene_id) {
//为临时二维码
if($type == 'temp') {
$data = '{"expire_seconds": '.$expire_seconds.', "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
} else {
//为永久二维码
$data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": '.$scene_id.'}}}';
}
$url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->getAccesstoken();
return curl_request($url, true, 'post', $data);
}
/**
* 获取二维码图片
* @param int $expire_seconds 有效期
* @param string $type 二维码类型
* @param int $scene_id 场景编号Id
* @param bool $save_file 是否保存到文件
* @return string
*/
public function getQRCode($scene_id=1, $type='temp', $expire_seconds=604800, $save_file=false) {
$json = json_decode($this->getTicket($expire_seconds, $type, $scene_id));
//dbg:var_dump($json);exit;
$ticket = $json->ticket;
$url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);//urlencode为安全
//dbg:exit($url);
$image = curl_request($url);
/*网页显示要加头: header('Content-Type:image/jpeg');*/
//保存二维码图片(便于下次使用)
if($save_file) {
$file = './'.$type.$scene_id.'.jpg';
file_put_contents($file, $image);
exit('File saved!');
} else {
exit($image);
}
}
/**
* 获取用户OpenId列表
* @param string $first_openid 起始用户Id,不填从第一个开始
* @return object
*/
public function getUserlist($first_openid = '') {
$url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token='.$this->getAccesstoken().
(!empty($first_openid)?'&next_openid='.$first_openid:'');
$result = curl_request($url);
return json_decode($result);
}
/**
* 获取用户基本信息
* @param string $open_id
* @return object
*/
public function getUserInfo($open_id) {
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$this->getAccesstoken().
'&openid='.$open_id.'&lang=zh_CN';
$result = curl_request($url);
return json_decode($result);
}
/**
* 获取网页OAuth2.0的用户信息
* @param type $only_openid true:仅获取openid即可, false:获取详细用户信息(注意:后者调用前入口API改 scope=snsapi_userinfo, 否则报错)
* @return mixed 错误时返回false
*/
public function getOAuth2UserInfo($only_openid = true) {
//必须传入微信服务器提供的code
if(!isset($_GET['code'])) {
exit( '错误:参数错误!');
#return false;
}
//获取openid和网页access_token
$code = $_GET['code'];
$access_token = '';
$openid = '';
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->_appid.'&secret='.$this->_appsecret.
'&code='.$code.'&grant_type=authorization_code';
$result = curl_request($url);
$r_obj = json_decode($result);
if(property_exists($r_obj, 'errcode')) {
echo '错误:'.$r_obj->errmsg;
return false;
} else {
$access_token = $r_obj->access_token;
$openid = $r_obj->openid;
}
if( $only_openid) {
return $openid;
}
//获取用户详细信息
else {
$url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
$result = curl_request($url);
$r_obj = json_decode($result);
if(property_exists($r_obj, 'errcode')) {
echo '错误:'.$r_obj->errmsg;
return false;
} else {
return $r_obj;
}
}
}
/**
* 获取OAuth2所需入口转跳url(人工单独调用)
* @param type $redir_url 我们目标地址
* @param type $scope 取值snsapi_base | snsapi_userinfo
* @param type $state 转跳后所传参数
*/
public function getOAuth2EnvelopUrl($redir_url, $scope='snsapi_base', $state=0) {
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.APPID.
'&redirect_uri='. urlencode($redir_url).
'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
return $url;
}
/**
* 获取网页jsapi的配置数组
* @return array
*/
public function getJswxConfig() {
$nonce_str = $this->createNonceStr();
$timestamp = time();
//动态获得url
$url = $protocol =
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443)
? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
//获得signatuere
$jsapi_ticket = $this->getJsApiTicket();
$string = "jsapi_ticket=$jsapi_ticket&noncestr=$nonce_str×tamp=$timestamp&url=$url";
$signature = sha1($string);
//返回config
$config = array(
"appId" => $this->_appid,
"nonceStr" => $nonce_str,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string,
);
return $config;
}
/**
* 官方创建随机字符串
* @param int $length
* @return string
*/
private function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 获取网页jsapi的ticket
* @return string
*/
private function getJsApiTicket() {
if( $jsapi_ticket = $this->getCacheValue('jsapi_ticket')) {
return $jsapi_ticket;
}
//重新获取
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token='.
$this->getAccesstoken();
$content = curl_request($url);
$js_obj = json_decode($content);
$this->setCacheValue('jsapi_ticket', $js_obj->ticket, $js_obj->expires_in);
return $js_obj->ticket;
}
/**
* 用于第一次公众服务器验证我们URL合法性
*/
public function firstValid() {
if($this->checkSignature()) {
echo $_GET['echostr']; //告知签名合法
} else {
echo 'failed!';
}
}
/**
* 微信官方的签名验证算法
* @return bool
*/
private function checkSignature() {
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = $this->_token;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING); //数字也按字典顺序排序
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
//debug: echo $tmpStr; exit;
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
/**
* 响应公众平台消息方法
*/
public function responseMsg() {
//签名验证
if(!$this->checkSignature()) exit('unsigned!');
//获得原始POST数据
$xml_str = $GLOBALS['HTTP_RAW_POST_DATA'];
if(empty($xml_str)) exit('failed!') ;
//解析xml,根据MsgType分发
libxml_disable_entity_loader(true); //防止外部实体注入(PHP 5 >= 5.2.11)
$msg_obj = simplexml_load_string($xml_str, 'SimpleXMLElement', LIBXML_NOCDATA);
return $msg_obj;
}
/**
* 公用返回文本
* @param type $from_user
* @param type $to_user
* @param type $content
*/
public function sendText($from_user, $to_user, $content) {
$response = '<xml>';
$response .= '<ToUserName><![CDATA['.$to_user.']]></ToUserName>';
$response .= '<FromUserName><![CDATA['.$from_user.']]></FromUserName>';
$response .= '<CreateTime>'.time().'</CreateTime>';
$response .= '<MsgType><![CDATA[text]]></MsgType>';
$response .= '<Content><![CDATA['.$content.']]></Content>';
$response .= '</xml>';
echo $response;
}
/**
* 公用返回图片
* @param type $from_user
* @param type $to_user
* @param string $file 文件位置
* @param bool $is_mediaid 上面给出的是MediaId(代表之前已经上传过)
*/
public function sendImage($from_user, $to_user, $file, $is_mediaid=false) {
if(!$is_mediaid) {
$r_obj = $this->uploadTmpMedia($file, 'image'); //上传素材
$media_id = $r_obj->media_id;
} else {
$media_id = $file;
}
$response = '<xml>';
$response .= '<ToUserName><![CDATA['.$to_user.']]></ToUserName>';
$response .= '<FromUserName><![CDATA['.$from_user.']]></FromUserName>';
$response .= '<CreateTime>'.time().'</CreateTime>';
$response .= '<MsgType><![CDATA[image]]></MsgType>';
$response .= '<Image>';
$response .= '<MediaId><![CDATA['.$media_id.']]></MediaId>';
$response .= '</Image>';
$response .= '</xml>';
echo $response;
}
/**
* 公用返回音乐
* @param string $from_user
* @param string $to_user
* @param string $music_url 音乐在公网的url
* @param string $music_hq_url 高品质在公网的url
* @param string $thunb_pic 缩略图的位置或MediaId
* @param bool $thumb_is_id 缩略图的位置或MediaId
* @param type $title 标题
* @param type $desc 描述
*/
public function sendMusic($from_user, $to_user, $music_url, $music_hq_url, $thunb_pic, $thumb_is_id=false, $title='',$desc='') {
if(!$thumb_is_id) {
$r_obj = $this->uploadTmpMedia($thunb_pic, 'image'); //上传素材
$media_id = $r_obj->media_id;
} else {
$media_id = $thunb_pic;
}
$response = '<xml>';
$response .= '<ToUserName><![CDATA['.$to_user.']]></ToUserName>';
$response .= '<FromUserName><![CDATA['.$from_user.']]></FromUserName>';
$response .= '<CreateTime>'.time().'</CreateTime>';
$response .= '<MsgType><![CDATA[music]]></MsgType>';
$response .= '<Music>';
$response .= '<Title><![CDATA['.$title.']]></Title>';
$response .= '<Description><![CDATA['.$desc.']]></Description>';
$response .= '<MusicUrl><![CDATA['.$music_url.']]></MusicUrl>';
$response .= '<HQMusicUrl><![CDATA['.$music_url.']]></HQMusicUrl>';
$response .= '<ThumbMediaId><![CDATA['.$media_id.']]></ThumbMediaId>';
$response .= '</Music>';
$response .= '</xml>';
echo $response;
}
/**
* 公用返回图文消息
* @param string $from_user
* @param string $to_user
* @param array $news_list 新闻列表
*/
public function sendNews($from_user, $to_user, $news_list=array()) {
//条目部分
$items = '';
foreach ($news_list as $k=>$v) {
$items .= '<item>';
$items .= '<Title><![CDATA['.$v['title'].']]></Title> ';
$items .= '<Description><![CDATA['.$v['description'].']]></Description>';
$items .= '<PicUrl><![CDATA['.$v['picurl'].']]></PicUrl>';
$items .= '<Url><![CDATA['.$v['url'].']]></Url>';
$items .= '</item>';
}
//整体部分
$response = '<xml>';
$response .= '<ToUserName><![CDATA['.$to_user.']]></ToUserName>';
$response .= '<FromUserName><![CDATA['.$from_user.']]></FromUserName>';
$response .= '<CreateTime>'.time().'</CreateTime>';
$response .= '<MsgType><![CDATA[news]]></MsgType>';
$response .= '<ArticleCount>'.count($news_list).'</ArticleCount>';
$response .= '<Articles>'.$items.'</Articles>';
$response .= '</xml>';
echo $response;
}
/**
* 公用发送模板消息(主动)
* @param string $to_user
* @param string $temp_id
* @param string $link
* @param array $data 模板里的变量数据,关联数组
* @return bool
*/
public function sendTemplateMsg($to_user, $temp_id, $link, $data) {
$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$this->getAccesstoken();
$post_arr = array(
"touser" => $to_user,
"template_id"=> $temp_id,
"url" => $link,
"data" => $data,
);
$post_str = json_encode($post_arr);
//dbg:echo $post_str; exit;
$result = curl_request($url, true, 'post', $post_str);
$r_obj = json_decode($result);
if($r_obj->errcode == 0) {
return true;
} else {
echo $r_obj->errmsg;
return false;
}
}
/**
* 公用发送客服消息(主动)
* 参考: https://mp.weixin.qq.com/wiki/11/c88c270ae8935291626538f9c64bd123.html
* @param string $to_user
* @param mixed $data
* @param string $type
* @param bool $ret_inst 先立即返回(否则微信会提示'微信号无法服务')
*/
public function sendCustomerMsg($to_user, $data, $type='text', $ret_inst=true) {
// 立即返回
if($ret_inst) {
//SAE的Apache使用以下代码无效
// ignore_user_abort(true);
// ob_start();
// echo 'success'; // send the response
// header('Connection: close');
// header('Content-Length: ' . ob_get_length());
// ob_end_flush();
// ob_flush();
// flush();
}
// 通过接口回复(暂时仅text)
$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$this->getAccesstoken();
$post_arr = array(
"touser" => $to_user,
"msgtype" => $type,
$type => array(
"content" => $data,
),
);
$post_str = json_encode($post_arr, JSON_UNESCAPED_UNICODE); //此接口中文不能转码
$result = curl_request($url, true, 'post', $post_str);
$r_obj = json_decode($result);
if($r_obj->errcode == 0) {
return true;
} else {
if(!$ret_inst) {
echo $r_obj->errmsg;
} else {
sae_debug($r_obj->errmsg);
}
return false;
}
}
/**
* 公用按分组群发文本消息(主动)
* @param string $content
* @param bool $is_to_all
* @param int $group_id
* @return boolean
*/
public function sendAllText($content, $is_to_all=true, $group_id=0) {
$url = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.$this->getAccesstoken();
$post_arr = array(
'filter' => array(
'is_to_all' => $is_to_all,
'group_id' => $group_id,
),
'text' => array(
'content' => $content,
),
'msgtype' => 'text',
);
$post_str = json_encode($post_arr,JSON_UNESCAPED_UNICODE); //JSON_UNESCAPED_UNICODE否则中文\u显示乱码
$result = curl_request($url, true, 'post', $post_str);
$r_obj = json_decode($result);
if($r_obj->errcode == 0) {
return true;
} else {
echo $r_obj->errmsg;
return false;
}
}
/**
* 调用小黄鸡聊天机器人回复
* @param type $msg_obj
*/
public function callXiaohuangjiSend($msg_obj, $txt) {
$url = 'http://www.niurenqushi.com/api/simsimi/';
//$data['txt'] = $txt; //编码按 multipart/form-data
$data = 'txt='.urlencode($txt); //必须编码按application/x-www-form-urlencoded
$ret = curl_request($url, false, 'post', $data);
$content = json_decode($ret)->text;
$this->sendText($msg_obj->ToUserName, $msg_obj->FromUserName, $content);
}
/**
* 上传临时素材
* @param string $file 文件位置
* @param string $type 媒体类型
* @return obj 上传后json结果
*/
public function uploadTmpMedia($file, $type) {
$url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='.
$this->getAccesstoken().'&type='.$type;
$data = array(
'media' => '@'.$file, //@代表后面是文件地址
);
$result = curl_request($url,true,'post',$data);
return json_decode($result);
}
/**
* 上传永久素材
* @param string $file 文件位置
* @param string $type 媒体类型
* @return obj 上传后json结果
*/
public function uploadStillMedia($file, $type) {
$url = 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token='.
$this->getAccesstoken().'&type='.$type;
$data = array(
'media' => '@'.$file, //@代表后面是文件地址
);
$result = curl_request($url,true,'post',$data);
return json_decode($result);
}
/**
* 删除菜单
* @return bool
*/
public function menuDelete() {
$url = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$this->getAccesstoken();
$result = curl_request($url);
$r_obj = json_decode($result);
return $r_obj->errcode==0?true:false;
}
/**
* 创建菜单
* @return bool
*/
public function menuCreate($menu) {
$data = $menu;
$url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$this->getAccesstoken();
$result = curl_request($url,true,'post',$data);
$r_obj = json_decode($result);
if($r_obj->errcode==0) {
return true;
} else {
echo $r_obj->errmsg,'<br/>';
return false;
}
}
}
/**
* curl通用请求方法
* @param string $url
* @param bool $https
* @param string $method
* @param mixed $data array|string
* @return mixed
*/
function curl_request($url, $https=true, $method='get', $data=null) {
$ch = curl_init(); //初始化,返回资源号
curl_setopt($ch,CURLOPT_URL,$url); //访问的url
curl_setopt($ch,CURLOPT_HEADER,false); //不需要头信息
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); //不输出到页面仅返回字串
if($https) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//不做服务器端认证(如支付时则需要)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//不做客户端认证
}
if($method == 'post') {
curl_setopt($ch, CURLOPT_POST, true); //设置post方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置post的数据
}
$content = curl_exec($ch);
if($content === false) {
echo $errmsg = curl_error($ch);
}
//执行访问
curl_close($ch); //关闭资源
return $content;
}