thinkphp 用第三方類庫 wechat.class.php 實現微信菜單、微信信息接收與返回、微信網頁登錄靜默授權和用戶手動授權。主要是功能實現,幫助一些入門選手實現基本功能。
thinkphp 用第三方類庫 wechat.class.php 實現微信菜單、微信信息接收與返回、微信網頁登錄靜默授權和用戶手動授權。主要是功能實現,幫助一些入門選手實現基本功能。
protected $AppID = ''; // AppID(應用ID)
protected $AppSecret = ''; // AppSecret
protected $Token = ''; // 微信後台填寫的TOKEN
protected $Crypt = ''; // 消息加密KEY(EncodingAESKey)
/*
微信第三方開發者模式 使用方法
服務器地址 : http://你的域名/index.php/Wx/wechat.html
Token : 自己定義
AppID : 微信公眾賬戶後台的AppID
AppSecret : 微信公眾賬戶後台的AppSecret
Crypt : 微信公眾賬戶後台的EncodingAESKey
*/
public function wechat(){
// 第三方發送消息給公眾平台
$options = array(
'token' => $this->Token, // 填寫你設定的key
'encodingaeskey' => $this->Crypt,
'appid' => $this->AppID,
'appsecret' => $this->AppSecret,
'debug' => false,
'logcallback' => false,
);
import("@.Library.wechat");
$weObj = new \Wechat($options);
$weObj->valid();
$type = $weObj->getRev()->getRevType();
//設置菜單
$newmenu = array(
"button"=>
array(
array('type'=>'view','name'=>'客戶案例','url'=>'http://www.vmonsion.com'),
array('name'=>'我的','sub_button'=>array(
array('type'=>'view','name'=>'我的網站','url'=>'http://www.vmonsion.com'),
array('type'=>'view','name'=>'個人資料','url'=>'http://www.vmonsion.com'),
)
),
array('type'=>'view','name'=>'關於我們','url'=>'http://www.vmonsion.com/index.php'),
)
);
// 公眾號菜單更新
//$weObj->getMenu($newmenu);
$weObj->createMenu($newmenu);
//$weObj->deleteMenu($newmenu);
//分解數據獲得常用字段
//$this->openid = $weObj->getRev()->getRevFrom();
$this->type = $weObj->getRev()->getRevType();
$this->data = $weObj->getRev()->getRevData();
$content = '[捂臉]'.$weObj->getRev()->getRevContent();
switch($type) {
// 文本消息
case \Wechat::MSGTYPE_TEXT:
file_put_contents('test.txt','TextMessage',FILE_APPEND);
$weObj->text($content)->reply();
exit;
break;
// 圖片消息
case \Wechat::MSGTYPE_IMAGE:
$weObj->text('This is a Picture?')->reply();
break;
// 位置消息
case \Wechat::MSGTYPE_LOCATION:
$weObj->text('what?GPS?')->reply();
exit;
break;
// 連接消息
case \Wechat::MSGTYPE_LINK:
break;
// 音樂消息
case \Wechat::MSGTYPE_MUSIC:
break;
// 圖文消息(推送過來的應該不存在這種類型,但是可以給用戶回複該類型消息)
case \Wechat::MSGTYPE_NEWS:
break;
// 音頻消息
case \Wechat::MSGTYPE_VOICE:
break;
// 視頻消息
case \Wechat::MSGTYPE_VIDEO:
break;
// 短視頻
case \Wechat::MSGTYPE_SHORTVIDEO:
break;
//事件消息:五種
case \Wechat::MSGTYPE_EVENT:
$event = $weObj->getRev()->getRevEvent();
$reply = $this->messageEvent($event);
break;
default:
$weObj->text("你好,我是微信小助手,很高興為您服務~")->reply();
}
}
// Wechat事件處理
public function messageEvent($event){
switch($event) {
// 訂閱
case \Wechat::EVENT_SUBSCRIBE:
break;
// 取消訂閱
case \Wechat::EVENT_UNSUBSCRIBE:
break;
// 掃描帶參數二維碼
case \Wechat::EVENT_SCAN:
break;
// 上報地理位置
case \Wechat::EVENT_LOCATION:
break;
// 菜單 - 點擊菜單跳轉鏈接
case \Wechat::EVENT_MENU_VIEW:
break;
// 菜單 - 點擊菜單拉取消息
case \Wechat::EVENT_MENU_CLICK:
break;
// 菜單 - 掃碼推事件(客戶端跳URL)
case \Wechat::EVENT_MENU_SCAN_PUSH:
break;
// 菜單 - 掃碼推事件(客戶端不跳URL)
case \Wechat::EVENT_MENU_SCAN_WAITMSG:
break;
// 菜單 - 彈出係統拍照發圖
case \Wechat::EVENT_MENU_PIC_SYS:
break;
// 菜單 - 彈出拍照或者相冊發圖
case \Wechat::EVENT_MENU_PIC_PHOTO:
break;
// 菜單 - 彈出微信相冊發圖器
case \Wechat::EVENT_MENU_PIC_WEIXIN:
break;
// 菜單 - 彈出地理位置選擇器
case \Wechat::EVENT_MENU_LOCATION:
break;
default:
break;
}
}
複製代碼
/* 微信登錄驗證,可以選擇方法一(靜默授權)或者方法二(用戶手動授權)
1、設置微信公眾號的菜單,訪問 http://你的域名/index.php/Wx/checkLogin.html
2、返回redirect_uri的授權域名,獲取$_GET['code']
3、通過code換取網頁授權access_token和openID
4、function loginWx() 就是最終獲取數據的方法
*/
public function checkLogin(){
// 方法一 Scope為snsapi_base基本授權
$result = $this->_baseAuth("http://www.kodjr.com/index.php/Wx/loginWx.html");
// 方法二 Scope為snsapi_userinfo用戶授權
//$result = $this->_userInfoAuth("http://www.kodjr.com/index.php/Wap/loginWx.html");
}
// 驗證是否邦定帳號了
public function loginWx(){
$code = $_GET['code'];
// 方法一
$tokenOpenID = $this->_baseToken($code);
///dump($tokenOpenID);
// 設置session
if(!session('user_wechat_auth')){
session('user_wechat_auth', $tokenOpenID);
}else{
}
// 方法二
//$userData = $this->_userInfoToken($code);
//$userInfo = $this->_getUserInfo($userData['access_token'],$userData['openid']);
}
// 方法一:Scope為snsapi_base基本授權
public function _baseAuth($redirect_url){
// 1.準備scope為snsapi_base網頁授權頁麵
$baseurl = urlencode($redirect_url);
$snsapi_base_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->AppID.'&redirect_uri='.$baseurl.'&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
// 2.靜默授權,獲取code
$code = $_GET['code'];
if( !isset($code) ){
header('Location:'.$snsapi_base_url);
}
}
public function _baseToken($code){
// 3.通過code換取網頁授權access_token和openID
$curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->AppID.'&secret='.$this->AppSecret.'&code='.$code.'&grant_type=authorization_code';
$content = $this->http_curl($curl);
$result = json_decode($content[1],true);
$result['state'] = $content[0];
return $result;
}
// 方法二:Scope為snsapi_userinfo用戶授權
public function _userInfoAuth($redirect_url){
//1.準備scope為snsapi_userInfo網頁授權頁麵
$redirecturl = urlencode($redirect_url);
$snsapi_userInfo_url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->AppID.'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
//2.用戶手動同意授權,同意之後,獲取code
$code = $_GET['code'];
if( !isset($code) ){
header('Location:'.$snsapi_userInfo_url);
}
}
public function _userInfoToken($code){
//3.通過code換取網頁授權access_token
$curl = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->AppID.'&secret='.$this->AppSecret.'&code='.$code.'&grant_type=authorization_code';
$content = $this->http_curl($curl);
$result = json_decode($content[1],true);
$result['state'] = $content[0];
//4.通過access_token和openid拉取用戶信息
$webAccess_token = $result->access_token;
$openid = $result->openid;
$userInfourl = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$webAccess_token.'&openid='.$openid.'&lang=zh_CN ';
$recontent = $this->http_curl($userInfourl);
$userInfo = json_decode($recontent,true);
return $userInfo;
}
public function http_curl($url, $method='POST', $postfields = null, $headers = array(), $debug = false)
{
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
$this->postdata = $postfields;
}
break;
}
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
$response = curl_exec($ci);
$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
if ($debug) {
echo "=====post data======\r\n";
var_dump($postfields);
echo '=====info=====' . "\r\n";
print_r(curl_getinfo($ci));
echo '=====$response=====' . "\r\n";
print_r($response);
}
curl_close($ci);
return array($http_code, $response);
}
————————————————
版權聲明:本文為CSDN博主「Alvin_扣扣953821815」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/m0_37412958/article/details/78442304