1: <?php
2: 3: 4: 5: 6: 7: 8: 9:
10: class WxRobot_SDK{
11:
12: 13: 14:
15: public static $_instance = null;
16:
17: public function __construct(){
18: include_once('weixin/weixin.class.php');
19: $this->options = get_option(WEIXIN_ROBOT_OPTIONS);
20: $this->info = wx_request_array();
21: $this->obj = new WeiXin_SDK($this->options['ai'], $this->options['as']);
22: }
23:
24: 25: 26: 27: 28:
29: public static function instance(){
30: if( is_null (self::$_instance)){
31: self::$_instance = new self();
32: }
33: return self::$_instance;
34: }
35:
36:
37: 38: 39: 40: 41: 42: 43: 44: 45:
46: public function toMsgText($Msg, $mode=1){
47: if($mode){
48: $c = strlen($Msg);
49: if($c > 2048){
50: $Msg = $this->byte_substr($Msg);
51: }
52: }
53: return $this->obj->toMsgText($this->info['FromUserName'], $this->info['ToUserName'], $Msg);
54: }
55:
56: 57: 58: 59: 60: 61: 62: 63: 64: 65:
66: public function toMsgTextAlink($alink, $suffix = ''){
67: $link_info = '';
68: foreach($alink as $k=>$v){
69: $_n = "<a href='{$v['link']}'>{$v['title']}</a>".$suffix;
70: $ret_n = $link_info.$_n;
71: $_c = strlen($ret_n);
72: if($_c > 2048){
73: return $this->toMsgText($link_info);
74: }else{
75: $link_info .= $_n;
76: }
77: }
78: return $this->toMsgText($link_info);
79: }
80:
81:
82: 83: 84: 85: 86: 87: 88: 89:
90: public function toMsgPic($MediaId){
91: return $this->obj->toMsgPic($this->info['FromUserName'], $this->info['ToUserName'], $MediaId);
92: }
93:
94:
95: 96: 97: 98: 99: 100: 101: 102:
103: public function toMsgVoice($MediaId){
104: return $this->obj->toMsgVoice($this->info['FromUserName'], $this->info['ToUserName'], $MediaId);
105: }
106:
107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117:
118: public function toMsgMusic($title, $desc, $MusicUrl, $HQMusicUrl, $ThumbMediaId=''){
119: return $this->obj->toMsgMusic($this->info['FromUserName'], $this->info['ToUserName'], $title, $desc, $MusicUrl, $HQMusicUrl, $ThumbMediaId);
120: }
121:
122: 123: 124: 125: 126: 127: 128:
129: public function toMsgVideo($media_id, $thumb_media_id){
130: return $this->obj->toMsgVideo($this->info['FromUserName'], $this->info['ToUserName'], $media_id, $thumb_media_id);
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: public function toMsgTextPic($picTextInfo){
181: $fromUserName = $this->info['FromUserName'];
182: $toUserName = $this->info['ToUserName'];
183: return $this->obj->toMsgNews($fromUserName, $toUserName, $picTextInfo);
184: }
185:
186: 187: 188: 189: 190: 191: 192:
193: public function toMsgTextPicList($list){
194: $info = array();
195: foreach($list as $k=>$v){
196: $a['title'] = $v['title'];
197: $a['desc'] = $v['desc'];
198: $a['link'] = $v['link'];
199: $info[] = $a;
200: }
201: return $this->toMsgTextPic($info);
202: }
203:
204: 205: 206: 207: 208:
209: public function getReToken(){
210: $data = $this->obj->getToken();
211: $data = json_decode($data, true);
212: $data['expires_in'] = time() + $data['expires_in'];
213: $this->options['weixin_robot_token'] = json_encode($data);
214: update_option(WEIXIN_ROBOT_OPTIONS, $this->options);
215: return $data['access_token'];
216: }
217:
218: 219: 220: 221: 222:
223: public function getToken(){
224: if(empty($this->options['ai']) || empty($this->options['as'])){
225: wx_notice_msg('填写服务号的信息!!!');
226: }
227:
228: if(!empty($this->options['weixin_robot_token'])){
229: $data = $this->options['weixin_robot_token'];
230: $data = base64_decode($data);
231: $data = json_decode($data, true);
232: if($data['expires_in'] <= time()){
233: $_data = $this->obj->getToken();
234: $data = json_decode($_data, true);
235: if(isset($data['errcode'])){
236: return($_data);
237: }
238: $data['expires_in'] = time() + $data['expires_in'];
239: $this->options['weixin_robot_token'] = base64_encode(json_encode($data));
240: update_option(WEIXIN_ROBOT_OPTIONS, $this->options);
241: }
242: return $data['access_token'];
243: }else{
244: $_data = $this->obj->getToken();
245: $data = json_decode($_data, true);
246: if(isset($data['errcode'])){
247: return($_data);
248: }
249: $data['expires_in'] = time() + $data['expires_in'];
250: $this->options['weixin_robot_token'] = base64_encode(json_encode($data));
251: update_option(WEIXIN_ROBOT_OPTIONS, $this->options);
252: }
253: return $data['access_token'];
254: }
255:
256: 257: 258: 259:
260: 261: 262: 263: 264: 265: 266: 267: 268: 269:
270: public function getCustomServiceLog($open_id, $starttime, $endtime, $pagesize=20, $pageindex=1){
271: $token = $this->getToken();
272: return $this->obj->getCustomServiceLog($token, $open_id, $starttime, $endtime, $pagesize, $pageindex);
273: }
274:
275:
276:
277: 278: 279: 280: 281:
282: public function menuDel(){
283: $token = $this->getToken();
284: $data = $this->obj->menuDel($token);
285: return $data;
286: }
287:
288: 289: 290: 291: 292: 293:
294: public function menuSet($json){
295: $token = $this->getToken();
296: return $this->obj->menuSet($token, $json);
297: }
298:
299: 300: 301: 302: 303: 304:
305: public function menuGet(){
306: $token = $this->getToken();
307: $data = $this->obj->menuGet($token);
308: $data = json_decode($data, true);
309: if(isset($data['errcode'])){
310: if('46003' == $data['errcode']){
311: return true;
312: }
313: return false;
314: }
315: return $data;
316: }
317:
318:
319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331:
332: public function getSemantic($query, $appid, $uid, $city='北京', $category='flight,hotel', $region ='', $lat = '', $long = ''){
333: $token = $this->getToken();
334: return $this->obj->getSemantic($token, $query, $category, $lat, $long, $city, $region, $appid, $uid);
335: }
336:
337:
338:
339: 340: 341: 342: 343: 344: 345:
346: public function pushMsgText($open_id, $msg){
347: $token = $this->getToken();
348: return $this->obj->pushMsgText($token, $open_id, $msg);
349: }
350:
351: 352: 353: 354: 355: 356: 357:
358: public function pushMsgImage($open_id, $media_id){
359: $token = $this->getToken();
360: return $this->obj->pushMsgImage($token, $open_id, $media_id);
361: }
362:
363: 364: 365: 366: 367: 368: 369:
370: public function pushMsgImageAdv($open_id, $file){
371: if(filesize($file) > 131072){
372: return '{errcode: "file size too big"}';
373: }
374: $token = $this->getToken();
375: return $this->obj->pushMsgImageAdv($token, $open_id, $file);
376: }
377:
378: 379: 380: 381: 382: 383: 384:
385: public function pushMsgVoice($open_id,$media_id){
386: $token = $this->getToken();
387: return $this->obj->pushMsgVoice($token, $open_id, $media_id);
388: }
389:
390: 391: 392: 393: 394: 395: 396:
397: public function pushMsgVoiceAdv($open_id, $file){
398: if(filesize($file) > 262144){
399: return '{errcode: "file size too big"}';
400: }
401: $token = $this->getToken();
402: return $this->obj->pushMsgVoiceAdv($token, $open_id, $file);
403: }
404:
405: 406: 407: 408: 409: 410: 411: 412: 413:
414: public function pushMsgVideo($open_id, $media_id, $title, $desc){
415: $token = $this->getToken();
416: return $this->obj->pushMsgVoice($token, $open_id, $media_id);
417: }
418:
419: 420: 421: 422: 423: 424: 425: 426: 427:
428: public function pushMsgVideoAdv($open_id, $file, $title, $desc){
429: if(filesize($file) > 1048576){
430: return '{errcode: "file size too big"}';
431: }
432: $token = $this->getToken();
433: return $this->obj->pushMsgVoiceAdv($token, $file,$open_id, $file);
434: }
435:
436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446:
447: public function pushMsgMusic($open_id, $file, $title, $desc, $musicurl, $hqmusicurl){
448: $token = $this->getToken();
449: return $this->obj->pushMsgMusic($token, $open_id, $thumb_media_id, $title, $desc, $musicurl, $hqmusicurl);
450: }
451:
452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462:
463: public function pushMsgMusicAdv($open_id, $file, $title, $desc, $musicurl, $hqmusicurl){
464: if(filesize($file) > 65536){
465: return '{errcode: "file size too big"}';
466: }
467: $token = $this->getToken();
468: return $this->obj->pushMsgMusicAdv($token, $open_id, $file, $title, $desc, $musicurl, $hqmusicurl);
469: }
470:
471:
472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483:
484: public function pushMsgNew($open_id, $info){
485: $token = $this->getToken();
486: return $this->obj->pushMsgNew($token, $open_id, $info);
487: }
488:
489:
490:
491: 492: 493: 494: 495: 496:
497: public function download($media_id){
498: $token = $this->getToken();
499: return $this->obj->download($token, $media_id);
500: }
501:
502: 503: 504: 505: 506: 507: 508:
509: public function upload($type, $file){
510: $token = $this->getToken();
511: return $this->obj->upload($token, $type, $file);
512: }
513:
514: 515: 516: 517: 518: 519: 520: 521: 522: 523:
524:
525:
526: 527: 528: 529: 530: 531: 532:
533: public function getUserInfo($open_id, $lang='zh_CN'){
534: $token = $this->getToken();
535: return $this->obj->getUserInfo($token, $open_id);
536: }
537:
538: 539: 540: 541: 542: 543:
544: public function getUserList($next_openid = ''){
545: $token = $this->getToken();
546: return $this->obj->getUserList($token, $next_openid);
547: }
548:
549: 550: 551: 552: 553:
554: public function setUserGroup($json){
555: $token = $this->getToken();
556: return $this->obj->setUserGroup($token, $json);
557: }
558:
559: 560: 561: 562: 563:
564: public function getUserGroup(){
565: $token = $this->getToken();
566: return $this->obj->getUserGroup($token);
567: }
568:
569: 570: 571: 572: 573: 574:
575: public function getUserGroupPosition($json){
576: $token = $this->getToken();
577: return $this->obj->getUserGroupPosition($token, $json);
578: }
579:
580: 581: 582: 583: 584: 585:
586: public function modUserGroup($json){
587: $token = $this->getToken();
588: return $this->obj->modUserGroup($token, $json);
589: }
590:
591: 592: 593: 594: 595: 596:
597: public function movUserGroup($json){
598: $token = $this->getToken();
599: return $this->obj->movUserGroup($token, $json);
600: }
601:
602: 603: 604: 605: 606: 607: 608:
609: public function updateRemark($open_id, $remark){
610: $token = $this->getToken();
611: return $this->obj->updateRemark($token, $open_id, $remark);
612: }
613:
614:
615:
616:
617: 618: 619: 620: 621: 622: 623: 624:
625: public function get_ticket_url($scene_id = 'midoks', $time = '3000', $type = 'temp'){
626: $url = '';
627: if('temp' == $type){
628: $tmp_ticket = array(
629: 'expire_seconds'=> $time,
630: 'action_name'=>'QR_SCENE',
631: 'action_info'=> array('scene'=> array('scene_id'=> $scene_id)),
632: );
633: $data = $this->temp_ticket($tmp_ticket);
634: $data = json_decode($data, true);
635: $url = $this->get_ticket($data['ticket']);
636: }else if('permanent' == $type){
637: $tmp_ticket = array(
638: 'action_name'=>'QR_LIMIT_SCENE',
639: 'action_info'=> array('scene'=> array('scene_id'=> $scene_id)),
640: );
641: $data = $this->permanent_ticket($tmp_ticket);
642: $data = json_decode($data, true);
643: $url = $this->get_ticket($data['ticket']);
644: }
645: return $url;
646: }
647:
648: 649: 650: 651: 652: 653:
654: public function temp_ticket($json){
655: $token = $this->getToken();
656: return $this->obj->temp_ticket($token, $json);
657: }
658:
659: 660: 661: 662: 663: 664:
665: public function permanent_ticket($json){
666: $token = $this->getToken();
667: return $this->obj->permanent_ticket($token, $json);
668: }
669:
670: 671: 672: 673: 674:
675: public function get_ticket($ticket){
676: return $this->obj->get_ticket($ticket);
677: }
678:
679: 680: 681: 682: 683: 684:
685: public function long2short($url){
686: $token = $this->getToken();
687: return $this->obj->long2short($token, $url);
688: }
689:
690:
691:
692:
693:
694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708:
709: public function sendGroupInfo(array $json){
710:
711: $groups = $this->getUserGroup();
712: $_groups = json_decode($groups, true);
713: if(!isset($_groups['groups'])){
714: return $_groups;
715: }
716: $_max = 0;
717: $_id = null;
718:
719: foreach ($_groups['groups'] as $v) {
720: if($v['count'] > $_max){
721: $_max = $v['count'];
722: $_id = $v['id'];
723: }
724: }
725:
726: if(is_numeric($_id)){
727: $_json = array();
728: foreach($json as $k=>$v){
729: $image = $this->upload('thumb', $v['image']);
730: $image = json_decode($image, 'true');
731: if (!isset($image['thumb_media_id'])) {
732: return $image;
733: }
734:
735: $_insert = array();
736: $_insert['thumb_media_id'] = $image['thumb_media_id'];
737: $_insert['author'] = $v['author'];
738: $_insert['title'] = $v['title'];
739: $_insert['content_source_url'] = $v['content_source_url'];
740: $_insert['digest'] = $v['digest'];
741: $_insert['show_cover_pic'] = $v['show_cover_pic'];
742: $_json[] = $_insert;
743: }
744: $imageText = $this->uploadMsgImageText($_json);
745: $imageText = json_decode($imageText, 'true');
746: if(!isset($imageText['media_id'])){
747: return $imageText;
748: }
749: return $this->sendAllByGroup($_id, $imageText['media_id']);
750: }
751: return false;
752: }
753:
754: 755: 756: 757: 758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770:
771: public function uploadMsgImageText(array $msg){
772: $num = count($msg);
773: if($num>10){
774: $_msg = array_slice($msg, 0, 9);
775: }else{
776: $_msg = $msg;
777: }
778: $token = $this->getToken();
779: return $this->obj->uploadMsgImageText($token, $_msg);
780: }
781:
782: 783: 784: 785: 786: 787: 788: 789:
790: public function sendAllByGroup($group_id, $media_id, $msgtype = 'mpnews'){
791: $token = $this->getToken();
792: return $this->obj->sendAllByGroup($token, $group_id, $media_id, $msgtype);
793: }
794:
795: 796: 797: 798: 799: 800: 801:
802: public function sendAll($user, $media_id, $msgtype = 'mpnews'){
803: $token = $this->getToken();
804: return $this->obj->sendAll($token, $user, $media_id, $msgtype);
805: }
806:
807: 808: 809: 810: 811: 812:
813: public function deleteSend($id){
814: $token = $this->getToken();
815: return $this->obj->deleteSend($token, $id);
816: }
817:
818: 819: 820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850:
851: public function sendTemplateInfo(array $json){
852: $token = $this->getToken();
853: return $this->obj->sendTemplateInfo($token, $json);
854: }
855:
856: 857: 858: 859: 860:
861: public function getWeixinIp(){
862: $token = $this->getToken();
863: return $this->obj->getWeixinIp($token);
864: }
865:
866:
867:
868: 869: 870: 871: 872: 873:
874: public function byte_substr($str, $len = 2048){
875: $ret = '';
876: $c = strlen($str);
877: for($i=0; $i<$c; $i++){
878: if(ord(substr($str, $i, 1)) > 0xa0){
879: $temp_wd = substr($str, $i, 3);
880: $i += 2;
881:
882: $temp_len = strlen($ret);
883: if(($temp_len+3)>$len){
884: return $ret;
885: }else if(($temp_len+3) == $len){
886: return $ret.$temp_wd;
887: }else{
888: $ret .= $temp_wd;
889: }
890: }else{
891: $temp_wd = substr($str, $i, 1);
892: $temp_len = strlen($ret);
893:
894: if(($temp_len+1)>$len){
895: return $ret;
896: }else if(($temp_len+1) == $len){
897: return $ret.$temp_wd;
898: }else{
899: $ret .= $temp_wd;
900: }
901: }
902: }
903: return $ret;
904: }
905:
906: 907: 908: 909: 910: 911:
912: public function to_json($arr){
913: return $this->obj->to_json($arr);
914: }
915:
916: }
917: ?>
918: