自己整理的微信公眾號類比較全 從認證配置到呼叫各種介面都能用的
阿新 • • 發佈:2019-01-03
<?php namespace Org\Util; class Wechat{ const MSGTYPE_TEXT = 'text'; const MSGTYPE_IMAGE = 'image'; const MSGTYPE_LOCATION = 'location'; const MSGTYPE_LINK = 'link'; const MSGTYPE_EVENT = 'event'; const MSGTYPE_MUSIC = 'music'; const MSGTYPE_NEWS = 'news'; const MSGTYPE_VOICE = 'voice'; const MSGTYPE_VIDEO = 'video'; const EVENT_SUBSCRIBE = 'subscribe'; //訂閱 const EVENT_UNSUBSCRIBE = 'unsubscribe'; //取消訂閱 const EVENT_SCAN = 'SCAN'; //掃描帶引數二維碼 const EVENT_LOCATION = 'LOCATION'; //上報地理位置 const EVENT_MENU_VIEW = 'VIEW'; //選單 - 點選選單跳轉連結 const EVENT_MENU_CLICK = 'CLICK'; //選單 - 點選選單拉取訊息 const EVENT_MENU_SCAN_PUSH = 'scancode_push'; //選單 - 掃碼推事件(客戶端跳URL) const EVENT_MENU_SCAN_WAITMSG = 'scancode_waitmsg'; //選單 - 掃碼推事件(客戶端不跳URL) const EVENT_MENU_PIC_SYS = 'pic_sysphoto'; //選單 - 彈出系統拍照發圖 const EVENT_MENU_PIC_PHOTO = 'pic_photo_or_album'; //選單 - 彈出拍照或者相簿發圖 const EVENT_MENU_PIC_WEIXIN = 'pic_weixin'; //選單 - 彈出微信相簿發圖器 const EVENT_MENU_LOCATION = 'location_select'; //選單 - 彈出地理位置選擇器 const EVENT_SEND_MASS = 'MASSSENDJOBFINISH'; //傳送結果 - 高階群發完成 const EVENT_SEND_TEMPLATE = 'TEMPLATESENDJOBFINISH';//傳送結果 - 模板訊息傳送結果 const EVENT_KF_SEESION_CREATE = 'kfcreatesession'; //多客服 - 接入會話 const EVENT_KF_SEESION_CLOSE = 'kfclosesession'; //多客服 - 關閉會話 const EVENT_KF_SEESION_SWITCH = 'kfswitchsession'; //多客服 - 轉接會話 const EVENT_CARD_PASS = 'card_pass_check'; //卡券 - 稽核通過 const EVENT_CARD_NOTPASS = 'card_not_pass_check'; //卡券 - 稽核未通過 const EVENT_CARD_USER_GET = 'user_get_card'; //卡券 - 使用者領取卡券 const EVENT_CARD_USER_DEL = 'user_del_card'; //卡券 - 使用者刪除卡券 const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin'; const AUTH_URL = '/token?grant_type=client_credential&'; const MENU_CREATE_URL = '/menu/create?'; const MENU_GET_URL = '/menu/get?'; const MENU_DELETE_URL = '/menu/delete?'; const GET_TICKET_URL = '/ticket/getticket?'; const CALLBACKSERVER_GET_URL = '/getcallbackip?'; const QRCODE_CREATE_URL='/qrcode/create?'; const QR_SCENE = 0; const QR_LIMIT_SCENE = 1; const QRCODE_IMG_URL='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='; const SHORT_URL='/shorturl?'; const USER_GET_URL='/user/get?'; const USER_INFO_URL='/user/info?'; const USER_UPDATEREMARK_URL='/user/info/updateremark?'; const GROUP_GET_URL='/groups/get?'; const USER_GROUP_URL='/groups/getid?'; const GROUP_CREATE_URL='/groups/create?'; const GROUP_UPDATE_URL='/groups/update?'; const GROUP_MEMBER_UPDATE_URL='/groups/members/update?'; const GROUP_MEMBER_BATCHUPDATE_URL='/groups/members/batchupdate?'; const CUSTOM_SEND_URL='/message/custom/send?'; const MEDIA_UPLOADNEWS_URL = '/media/uploadnews?'; const MASS_SEND_URL = '/message/mass/send?'; const TEMPLATE_SET_INDUSTRY_URL = '/message/template/api_set_industry?'; const TEMPLATE_ADD_TPL_URL = '/message/template/api_add_template?'; const TEMPLATE_SEND_URL = '/message/template/send?'; const MASS_SEND_GROUP_URL = '/message/mass/sendall?'; const MASS_DELETE_URL = '/message/mass/delete?'; const MASS_PREVIEW_URL = '/message/mass/preview?'; const MASS_QUERY_URL = '/message/mass/get?'; const UPLOAD_MEDIA_URL = 'http://file.api.weixin.qq.com/cgi-bin'; const MEDIA_UPLOAD_URL = '/media/upload?'; const MEDIA_GET_URL = '/media/get?'; const MEDIA_VIDEO_UPLOAD = '/media/uploadvideo?'; const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?'; const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?'; const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?'; const MEDIA_FOREVER_GET_URL = '/material/get_material?'; const MEDIA_FOREVER_DEL_URL = '/material/del_material?'; const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?'; const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?'; const OAUTH_PREFIX = 'https://open.weixin.qq.com/connect/oauth2'; const OAUTH_AUTHORIZE_URL = '/authorize?'; ///多客服相關地址 //const CUSTOM_SERVICE_GET_RECORD = '/customservice/getrecord?'; const CUSTOM_SERVICE_GET_RECORD = 'https://api.weixin.qq.com/customservice/msgrecord/getrecord?'; const CUSTOM_SERVICE_GET_KFLIST = '/customservice/getkflist?'; const CUSTOM_SERVICE_GET_ONLINEKFLIST = '/customservice/getonlinekflist?'; const API_BASE_URL_PREFIX = 'https://api.weixin.qq.com'; //以下API介面URL需要使用此字首 const OAUTH_TOKEN_URL = '/sns/oauth2/access_token?'; const OAUTH_REFRESH_URL = '/sns/oauth2/refresh_token?'; const OAUTH_USERINFO_URL = '/sns/userinfo?'; const OAUTH_AUTH_URL = '/sns/auth?'; ///多客服相關地址 const CUSTOM_SESSION_CREATE = '/customservice/kfsession/create?'; const CUSTOM_SESSION_CLOSE = '/customservice/kfsession/close?'; const CUSTOM_SESSION_SWITCH = '/customservice/kfsession/switch?'; const CUSTOM_SESSION_GET = '/customservice/kfsession/getsession?'; const CUSTOM_SESSION_GET_LIST = '/customservice/kfsession/getsessionlist?'; const CUSTOM_SESSION_GET_WAIT = '/customservice/kfsession/getwaitcase?'; const CS_KF_ACCOUNT_ADD_URL = '/customservice/kfaccount/add?'; const CS_KF_ACCOUNT_UPDATE_URL = '/customservice/kfaccount/update?'; const CS_KF_ACCOUNT_DEL_URL = '/customservice/kfaccount/del?'; const CS_KF_ACCOUNT_UPLOAD_HEADIMG_URL = '/customservice/kfaccount/uploadheadimg?'; // 新增 const CS_KF_ACCOUNT_Inviteworker = '/customservice/kfaccount/inviteworker?'; ///卡券相關地址 const CARD_CREATE = '/card/create?'; const CARD_DELETE = '/card/delete?'; const CARD_UPDATE = '/card/update?'; const CARD_GET = '/card/get?'; const CARD_BATCHGET = '/card/batchget?'; const CARD_MODIFY_STOCK = '/card/modifystock?'; const CARD_LOCATION_BATCHADD = '/card/location/batchadd?'; const CARD_LOCATION_BATCHGET = '/card/location/batchget?'; const CARD_GETCOLORS = '/card/getcolors?'; const CARD_QRCODE_CREATE = '/card/qrcode/create?'; const CARD_CODE_CONSUME = '/card/code/consume?'; const CARD_CODE_DECRYPT = '/card/code/decrypt?'; const CARD_CODE_GET = '/card/code/get?'; const CARD_CODE_UPDATE = '/card/code/update?'; const CARD_CODE_UNAVAILABLE = '/card/code/unavailable?'; const CARD_TESTWHILELIST_SET = '/card/testwhitelist/set?'; const CARD_MEMBERCARD_ACTIVATE = '/card/membercard/activate?'; //啟用會員卡 const CARD_MEMBERCARD_UPDATEUSER = '/card/membercard/updateuser?'; //更新會員卡 const CARD_MOVIETICKET_UPDATEUSER = '/card/movieticket/updateuser?'; //更新電影票(未加方法) const CARD_BOARDINGPASS_CHECKIN = '/card/boardingpass/checkin?'; //飛機票-線上選座(未加方法) const CARD_LUCKYMONEY_UPDATE = '/card/luckymoney/updateuserbalance?'; //更新紅包金額 const SEMANTIC_API_URL = '/semantic/semproxy/search?'; //語義理解 ///資料分析介面 static $DATACUBE_URL_ARR = array( //使用者分析 'user' => array( 'summary' => '/datacube/getusersummary?', //獲取使用者增減資料(getusersummary) 'cumulate' => '/datacube/getusercumulate?', //獲取累計使用者資料(getusercumulate) ), 'article' => array( //圖文分析 'summary' => '/datacube/getarticlesummary?', //獲取圖文群發每日資料(getarticlesummary) 'total' => '/datacube/getarticletotal?', //獲取圖文群發總資料(getarticletotal) 'read' => '/datacube/getuserread?', //獲取圖文統計資料(getuserread) 'readhour' => '/datacube/getuserreadhour?', //獲取圖文統計分時資料(getuserreadhour) 'share' => '/datacube/getusershare?', //獲取圖文分享轉發資料(getusershare) 'sharehour' => '/datacube/getusersharehour?', //獲取圖文分享轉發分時資料(getusersharehour) ), 'upstreammsg' => array( //訊息分析 'summary' => '/datacube/getupstreammsg?', //獲取訊息傳送概況資料(getupstreammsg) 'hour' => '/datacube/getupstreammsghour?', //獲取訊息分送分時資料(getupstreammsghour) 'week' => '/datacube/getupstreammsgweek?', //獲取訊息傳送週數據(getupstreammsgweek) 'month' => '/datacube/getupstreammsgmonth?', //獲取訊息傳送月資料(getupstreammsgmonth) 'dist' => '/datacube/getupstreammsgdist?', //獲取訊息傳送分佈資料(getupstreammsgdist) 'distweek' => '/datacube/getupstreammsgdistweek?', //獲取訊息傳送分佈週數據(getupstreammsgdistweek) 'distmonth' => '/datacube/getupstreammsgdistmonth?', //獲取訊息傳送分佈月資料(getupstreammsgdistmonth) ), 'interface' => array( //介面分析 'summary' => '/datacube/getinterfacesummary?', //獲取介面分析資料(getinterfacesummary) 'summaryhour' => '/datacube/getinterfacesummaryhour?', //獲取介面分析分時資料(getinterfacesummaryhour) ) ); ///微信搖一搖周邊 const SHAKEAROUND_DEVICE_APPLYID = '/shakearound/device/applyid?';//申請裝置ID const SHAKEAROUND_DEVICE_UPDATE = '/shakearound/device/update?';//編輯裝置資訊 const SHAKEAROUND_DEVICE_SEARCH = '/shakearound/device/search?';//查詢裝置列表 const SHAKEAROUND_DEVICE_BINDLOCATION = '/shakearound/device/bindlocation?';//配置裝置與門店ID的關係 const SHAKEAROUND_DEVICE_BINDPAGE = '/shakearound/device/bindpage?';//配置裝置與頁面的繫結關係 const SHAKEAROUND_MATERIAL_ADD = '/shakearound/material/add?';//上傳搖一搖圖片素材 const SHAKEAROUND_PAGE_ADD = '/shakearound/page/add?';//增加頁面 const SHAKEAROUND_PAGE_UPDATE = '/shakearound/page/update?';//編輯頁面 const SHAKEAROUND_PAGE_SEARCH = '/shakearound/page/search?';//查詢頁面列表 const SHAKEAROUND_PAGE_DELETE = '/shakearound/page/delete?';//刪除頁面 const SHAKEAROUND_USER_GETSHAKEINFO = '/shakearound/user/getshakeinfo?';//獲取搖周邊的裝置及使用者資訊 const SHAKEAROUND_STATISTICS_DEVICE = '/shakearound/statistics/device?';//以裝置為維度的資料統計介面 const SHAKEAROUND_STATISTICS_PAGE = '/shakearound/statistics/page?';//以頁面為維度的資料統計介面 private $token; private $encodingAesKey; private $encrypt_type; private $appid; private $appsecret; public $access_token; private $jsapi_ticket; private $user_token; private $partnerid; private $partnerkey; private $paysignkey; private $postxml; private $_msg; private $_funcflag = false; private $_receive; private $_text_filter = true; public $debug = false; public $errCode = 40001; public $errMsg = "no access"; public $logcallback; public function __construct($options) { $this->token = isset($options['token'])?$options['token']:''; $this->encodingAesKey = isset($options['encodingaeskey'])?$options['encodingaeskey']:''; $this->appid = isset($options['appid'])?$options['appid']:''; $this->appsecret = isset($options['appsecret'])?$options['appsecret']:''; $this->debug = isset($options['debug'])?$options['debug']:false; $this->logcallback = isset($options['logcallback'])?$options['logcallback']:false; } /** * For weixin server validation */ private function checkSignature($str='') { $signature = isset($_GET["signature"])?$_GET["signature"]:''; $signature = isset($_GET["msg_signature"])?$_GET["msg_signature"]:$signature; //如果存在加密驗證則用加密驗證段 $timestamp = isset($_GET["timestamp"])?$_GET["timestamp"]:''; $nonce = isset($_GET["nonce"])?$_GET["nonce"]:''; $token = $this->token; $tmpArr = array($token, $timestamp, $nonce,$str); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } public function test(){ echo '111'; } //=============未完========================
//這裡是config配置
'wechat'=>[
'appid'=>'wx2XXXXXX7dbb8d', //填寫高階呼叫功能的app id
'appsecret'=>'be4ea413XXXXXXXXe7315bc', //填寫高階呼叫功能的金鑰
'token'=>'ysqtest',
'oauthback' => 'http://www.XXX.com/index.php/index/Index/oauthback'
],
類庫檔案詳情請點選檢視//這裡是呼叫方法 用的tp-5 <?php namespace app\index\controller; use util\Wechat; class Index extends \app\index\controller\Home { public $wx = null; public $conf = null; public $wechat = null; private $noCkeck = ['oauth','test','oauth_back','oauthback','clear','getjssign','_a','login','check_mp']; public function _initialize(){ parent::_initialize(); define('ACTION_NAME' , request()->action()); define('__SELF__' , $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].url()); // error_reporting(0); $this->conf = Config('wechat'); $this->wechat = new Wechat($this->conf); if (!session('url')) { session('url',$_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI']); } $ac = ACTION_NAME; if(!in_array($ac,$this->noCkeck)){ $openid = session('openid'); if(empty($openid)){ $this->redirect('oauth'); } } } public function oauth(){ $url = $this->conf['oauthback']; $url = $this->wechat->getOauthRedirect($url);//,'snsapi_base'); // P($url); P(header('location:'.$url));//這是自己定義的函式 類似var_dump() $this->redirect($url); } public function clear(){ session_destroy(); } function oauthback(){ $accToken = $this->wechat->getOauthAccessToken(); $userInfo = $this->wechat->getOauthUserinfo($accToken['access_token'],$accToken['openid']);//這個不用關注都可以獲取使用者的資訊 $userInfo2 = $this->wechat->getUserInfo($accToken['openid']);//這個要關注才行 P($userInfo,0); P($userInfo2,1); } function getjssign(){ $url = $_POST['url']; $rs = $this->wechat->getJsSign($url); $this->_A(true,'操作成功!',$rs); } //這個是配置微信公眾號 --第一次握手 //這裡配置的是測試號 public function check_mp() { $this->wechat->valid(); } public function index() { return view(); }