thinkphp5微信公眾號二維碼掃碼關注推廣二維碼事件實現,
給出實現的全部方法:
獲取二維碼,讓微信公眾號跳轉到以下代碼的code方法即可,用戶點擊二維碼后,通過二維碼獲取關注用戶信息及二維碼推廣者信息,以及判斷用戶是否關注,具體代碼邏輯在以下代碼responseMsg方法,但需要在公眾平臺url配置接入到以下代碼的index方法,用戶關注后,則通過index方法訪問responseMsg方法。
```
<?php
namespace app\home\controller;
define("TOKEN", "xieqianghui1234");
use think\Controller;
use app\home\model\reIndex;
class Index extends Controller{
private $appId;
private $appSecret;
public function __construct()
{
$this->appId = 'wx17aaa90e1f107245';
$this->appSecret= '8bd13c549f07c6e365f818e113dee821';
}
// 獲取code
public function code(){
$appid = 'wx17aaa90e1f107245';
$redirecturl = urlencode("http://www.neophiledesign.com/kfgzh/public/index.php/home/index/member");
$url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirecturl.'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
header('location:'.$url);
}
// 通過ticket換取二維碼
public function wechatcode($ticket)
{
$url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
return $this->getjson($url);
}
// 二維碼呈現
public function member(){
$code = $_GET['code'];
$url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->appId.'&secret='.$this->appSecret.'&code='.$code.'&grant_type=authorization_code';
$res = $this->curl_post($url);
// 用戶openid
$openid = $res['openid'];
$recode = (new reIndex)->reselect($openid);
header("Content-Type: image/jpeg;text/html; charset=utf-8");
if(!$recode){
$rand = mt_rand(0,9).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);
$arr = array();
$arr = array(
'openid' => $openid,
'code' => $rand,
);
(new reIndex)->readd($arr);
$res = $this->reindex();
$access_token = $res['access_token'];
// 獲取ticket
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$list = array();
$list = array(
'action_name' => 'QR_LIMIT_SCENE',
'action_info' => array(
'scene' => array(
'scene_id' => $rand,
),
),
);
// 獲取ticket
$res = $this->curl_post($url,json_encode($list),'POST');
// 通過ticket換取二維碼
$res = $this->wechatcode($res['ticket']);
$file = fopen(dirname(dirname(dirname(__DIR__)))."/image/".$rand."upload.jpg","w");
fwrite($file,$res);//寫入
fclose($file);//關閉
$img = "http://www.neophiledesign.com/kfgzh/image/".$rand."upload.jpg";
}else{
$img = "http://www.neophiledesign.com/kfgzh/image/".$recode."upload.jpg";
}
echo "<img src=$img />";
}
// 獲取access_token
public function reindex(){
$appid = 'wx17aaa90e1f107245';
$secret = '8bd13c549f07c6e365f818e113dee821';
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$res = $this->curl_post($url);
return $res;
}
// 獲取用戶信息及openid
public function retest($access_token){
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid=OPENID&lang=zh_CN';
return $this->getJson($url);
}
// token驗證
public function index(){
if ($this->checkSignature()==false) {
die('非法請求');
}
if (isset($_GET["echostr"])) {
$echostr = $_GET["echostr"];
echo $echostr;
exit();
} else {
$this->responseMsg();
}
}
public function responseMsg(){
//get post data, May be due to the different environments
$postStr = file_get_contents("php://input");
if (!empty($postStr)){
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
// $keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
if($postObj->MsgType == 'event'){
$res = $this->reindex();
// 獲取access_token
$access_token = $res['access_token'];
$msgType = "text";
// 通過二維碼進入
if(isset($postObj->EventKey) && $postObj->EventKey != ''){
$tgzid = $postObj->EventKey;
if(substr($tgzid,8)){
$retgzid = substr($tgzid,8);
$openid = (new reIndex)->rereselect($retgzid);
if($openid != $fromUsername){
$sharesum = (new reIndex)->reupdate($openid);
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$fromUsername.'&lang=zh_CN';
$re = json_decode($this->getjson($url),true);
$contentStr = '你好,'.$re['nickname'].'歡迎關注香港葵芳!';
$url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$access_token;
$data = array();
$data = array(
"touser" => $openid,
"msgtype" => "text",
"text" =>
array(
"content" => "已成功分享給用戶".$re['nickname']."已成功分享".$sharesum.'次'
)
);
$res = $this->getjson($url,json_encode($data,JSON_UNESCAPED_UNICODE));
var_dump($res);
}
}else{
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$fromUsername.'&lang=zh_CN';
$re = json_decode($this->getjson($url),true);
$contentStr = '你好,'.$re['nickname'].'歡迎關注香港葵芳!';
}
}else{
if($postObj->Event == 'subscribe'){
$url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token='.$access_token.'&openid='.$fromUsername.'&lang=zh_CN';
$re = json_decode($this->getjson($url),true);
$contentStr = '你好,'.$re['nickname'].'歡迎關注香港葵芳!';
}
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}else {
exit;
}
}
//
public function curl_post($url, $data=null,$method='GET', $https=true)
{
// 創建一個新cURL資源
$ch = curl_init();
// 設置URL和相應的選項
curl_setopt($ch, CURLOPT_URL, $url);
//要訪問的網站 //啟用時會將頭文件的信息作為數據流輸出。
curl_setopt($ch, CURLOPT_HEADER, false);
//將curl_exec()獲取的信息以字符串返回,而不是直接輸出。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if($https){
//FALSE 禁止 cURL 驗證對等證書(peer's certificate)。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//驗證主機 }
if($method == 'POST'){
curl_setopt($ch, CURLOPT_POST, true);
//發送 POST 請求 //全部數據使用HTTP協議中的 "POST" 操作來發送。
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
// 抓取URL并把它傳遞給瀏覽器
$content = curl_exec($ch);
//關閉cURL資源,并且釋放系統資源
curl_close($ch);
return json_decode($content,true);
}
}
public function getjson($url,$data=null)
{
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_HEADER,false);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
//不為空,使用post傳參數,否則使用get
if($data){
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
}
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
```
- 空白目錄
- thinkcmf的權限管理
- thinkcmf+unicmf添加頁面
- Thinkphp5做后臺 Uni-app做前臺解決跨域問題
- 組件
- h5跨域-uniapp
- thinkphp5 auth 教程
- thinkphp5Auth類
- uniapp添加與編輯的差別
- 常見的請求方式
- uni 單選回顯數據_uniapp 頁面跳轉傳值和接收
- uni-app 單選/多選/滑動 demo
- 關于uniapp checkbox多選框如何傳值傳數據
- uniApp 多選框checkbox ,判斷是否選中
- uniapp添加復選框和獲取復選框的值
- uni-app中全選多選單選
- uniapp多選框CheckBox 數據接收
- uniapp下拉列表單選框復選框實戰demo(編輯或詳情頁)
- uni-data-CheckBox-OK
- js 字符串數組轉換成數字數組
- js把字符串轉為數組對象
- js中數組對象字符串的相互轉換
- JS怎么把字符串數組轉換成整型數組
- 小程序開發
- tp5.1跨域請求
- uniapp-h5跨域
- 新增
- order
- uni-app中調取接口的三種方式與封裝uni.request()
- uView-checkbox
- 給u-view的u-select賦值
- uView-下拉框、復選框、單選框 數據發送及接收
- CURD操作
- thinkphp5.1增刪改查
- TP5.1添加數據成功之后返回自增主鍵id
- Thinkphp實戰之Request默認值except only 以及過濾參
- uni-app跨域解決方案
- thinkphp5.1+uni-app接口開發中跨域問題解決方案
- tp6 + uniapp 前后端跨域解決方案
- uniapp-token相關
- uniapp request請求封裝包含token兼容多端,簡單易用
- CORS.php
- ThinkPHP6 API開發前后端分離用戶信息保存在后端的方法
- thinkphp的jwt(JSON Web Token)身份驗證
- thinkphp6增刪改查
- PHP模擬GET,POST請求
- php模擬get、post發送請求的6種方法
- thinkphp6
- uniapp封裝網絡請求
- thinkphp6搭建后端api接口jwt-auth
- uniapp實現APP微信登錄流程
- [uni-app] 中保持用戶登錄狀態
- 詳解vue中localStorage的使用方法
- vue 實現通過vuex 存儲值 在不同界面使用
- dispatch:異步操作,數據提交至 actions ,可用于向后臺提交數據
- ThinkPHP6.0 + Vue + ElementUI + axios 的環境安裝到實現 CURD 操作
- tp6錯誤集
- TP6 模型插入/添加數據,自動插入時間(自動時間戳)
- 手機不開機維修思路
- thinkphp6解決vue跨域問題
- 從0基礎獲取短視頻去水印解析接口制作
- thinkphp5 刪除緩存
- thinkPHP,怎么把json文件里面的數據導入數據庫
- 數字轉字符php
- php – 直接用curl下載遠程文件
- thinkphp – 直接用curl下載遠程文件
- apiAdmin安裝
- echart
- thinkphp開發小程序推廣分享帶參數二維碼生成
- php同比增加函數
- PHP獲取同比上周、上一個月,上一個季度,去年時間區間
- “前3秒”金句100例,趕緊收藏起來!
- PHP配合微信公眾號生成推廣二維碼
- thinkphp5+php微信公眾號二維碼掃碼關注推廣二維碼事件實現
- 獲取當前時間上一周的開始時間和結束時間
- TP6 查找指定工作日
- PHP 獲取當天、近一周、本周、上月、本月、本季度、上季度時間方法大全
- php獲取今日、昨日、本周、本月 日期方法
- Tp5+mysql按年季度月周日小時查詢時無數據的時間段補0方法
- mysql按天統計的時候,該天沒有數據也要統計為0
- 列出一星期的日期 無數據補0
- thinkphp6本周 上周 周一 周末日期
- 補全日期 無數據補0
- php+pv統計代碼實現,Laravel 10 行代碼實現簡單的網站 pv uv 統計
- 通過API獲取ip地址以及城市和運營商
- 獲取訪客信息
- 13行代碼實現微信小程序設置概率觸發激勵視頻閱讀文章
- uniapp 微信小程序 獲取場景值和場景值個性化參數
- 微信小程序分享小程序碼的生成(帶參數)以及參數的獲取
- 小程序推廣分享帶參數二維碼生成
- uniapp微信小程序生成對應頁面二維碼
- uniapp獲取當前頁面url
- uniapp微信小程序--微信登錄
- 微信小程序,生成小程序碼中scene參數的存放和獲取問題
- uni-app 微信小程序生成二維碼帶參數
- uni-app 微信小程序如何把圖片保存到本地相冊?
- thinkPHP5使用assign()傳遞富文本,前端解析成HTML標簽
- tp6解析編輯器里面的html標簽原樣輸出
- PHP判斷url鏈接是否被百度收錄
- 微擎安裝模塊時提示 Failed to connect to we7.rewlkj.com port 80: Timed out
- 小程序碼生成
- thinkphp開發小程序推廣分享帶參數二維碼生成0
- tp3.2偽靜態
- apiadmin安裝教程-2022.8更新
- autojs事件代碼
- uuuu
- thinkphp6: API 多版本控制