首頁  >  文章  >  後端開發  >  php實作微信掃碼自動登陸與註冊功能的方法

php實作微信掃碼自動登陸與註冊功能的方法

墨辰丷
墨辰丷原創
2018-06-01 10:43:091779瀏覽

這篇文章主要介紹了php實現微信掃碼自動登陸與註冊功能,結合實例形式分析了php微信二維碼識別接口與相關使用技巧,需要的朋友可以參考下

微信開發已經是現在程式設計師必須要掌握的一項基本的技術了,其實做過微信開發的都知道微信接口非常的強大做起來也非常的簡單,這裡我們一起來看一個微信自動登陸註冊的例子.

php 微信掃碼pc端自動登陸註冊用的介面scope 是snsapi_userinfo,微信登陸一個是網頁授權登陸,另一個是微信聯合登陸

網頁授權登陸:http ://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html

#微信聯合登陸:https://open.weixin.qq.com/cgi-bin/frame?t=home/frame?t=home/frame?t=home/frame?t=home/frame?t=home/frame?t=home/frame?t web_tmpl&lang=zh_CN

一、先把微信連結帶個識別產生二維碼

例如連結為https://open.weixin.qq.com/connect /oauth2/authorize?appid='.$appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'  我們可以在state上做文章,因為state你傳入什麼微信那邊回傳什麼

可以作為伺服器與微信段的一個標識:

public function creatqrAction(){
if($_GET['app']){
$wtoken=$_COOKIE['wtoken'];
$postdata=$_SESSION['w_state'];
if($wtoken){
$postdata=$wtoken;
}
include CONFIG_PATH . 'phpqrcode/'.'phpqrcode.php'
$sh=$this->shar1();
$value="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx138697ef383a9167&redirect_uri=http://www.xxx.net/login/wcallback&response_type=code&scope=snsapi_userinfo&state=".$postdata."&connect_redirect=1#wechat_redirect";
$errorCorrectionLevel = "L";
$matrixPointSize = "5";
QRcode::png($value, false, $errorCorrectionLevel, $matrixPointSize);
}
}

此時產生了二維碼state是標識,phpqrcode可以在文章結尾下載,這樣我們設定了回呼位址http://www.xxx.net/login/wcallback

#就可以在wcallback方法裡面處理資料插入使用者產生session,跳轉登陸,pc端可以設定幾秒鐘ajax請求伺服器,一旦取得到了state,即實現調整,微信瀏覽器裡處理完後可以關閉視窗,微信js可實現:

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
WeixinJSBridge.call('closeWindow');}, false);

也可以授權登陸成功後跳到微信服務號關注頁:

header("Location: weixin://profile/gh_a5e1959f9a4e");
wcallback方法做处理登陆
$code = $_GET['code'];
$state = $_GET['state'];
$setting = include CONFIG_PATH . 'setting.php'
$appid=$setting['weixin']['appid'];
$appsecret=$setting['weixin']['appsecret'];
if (emptyempty($code)) $this->showMessage('授权失败');
try{
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code'
$token = json_decode($this->https_request($token_url));
}catch(Exception $e)
{
print_r($e);
}
if (isset($token->errcode)) {
echo '错误:'.$token->errcode;
echo '错误信息:'.$token->errmsg;
exit;
}
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//转成对象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo '错误:'.$access_token->errcode;
echo '错误信息:'.$access_token->errmsg;
exit;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN'
//转成对象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo '错误:'.$user_info->errcode;
echo '错误信息:'.$user_info->errmsg;
exit;
}
//打印用户信息
// echo ''
// print_r($user_info);
// echo ''

#phpqrcode類別庫下載在此不提供各位可以百度搜尋下載

#magento微信掃碼網站自動登入的範例
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN

查看授權後介面呼叫(UnionID),不難發現填寫回呼位址,使用者確認登陸pc端即可跳轉

取得UnionID方法

public function wcallbackAction(){
$code = $_GET['code'];
$state = $_GET['state'];
$setting = include CONFIG_PATH . 'setting.php';
$appid=$setting['weixin']['appid'];
$appsecret=$setting['weixin']['appsecret'];
if (emptyempty($code)) $this->showMessage('授权失败');
try{
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$token = json_decode($this->https_request($token_url));
}catch(Exception $e)
{
print_r($e);
}
if (isset($token->errcode)) {
echo &#39;<h1>错误:</h1>&#39;.$token->errcode;
echo &#39;<br/><h2>错误信息:</h2>&#39;.$token->errmsg;
exit;
}
$access_token_url = &#39;https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=&#39;.$appid.&#39;&grant_type=refresh_token&refresh_token=&#39;.$token->refresh_token;
//转成对象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo &#39;<h1>错误:</h1>&#39;.$access_token->errcode;
echo &#39;<br/><h2>错误信息:</h2>&#39;.$access_token->errmsg;
exit;
}
$user_info_url = &#39;https://api.weixin.qq.com/sns/userinfo?access_token=&#39;.$access_token->access_token.&#39;&openid=&#39;.$access_token->openid.&#39;&lang=zh_CN&#39;;
//转成对象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo &#39;<h1>错误:</h1>&#39;.$user_info->errcode;
echo &#39;<br/><h2>错误信息:</h2>&#39;.$user_info->errmsg;
exit;
}
//打印用户信息
// echo &#39;<pre class="brush:php;toolbar:false">&#39;;
// print_r($user_info);
// echo &#39;
'; //获取unionid $uid=$user_info->unionid; } //用户操作处理 分为再次登录和第一次登陆 $sql="select h_user_id from dtb_user_binded as t1 left join dtb_user_weixin as t2 on t1.u_id=t2.id where t1.u_type='". User::$arrUtype['weixin_num_t']."' and t2.openid='$user_info->unionid'"; $h_user_id = Core_Db::getOne($sql); if(!emptyempty($h_user_id)){//该weixin号再次登录 }{//该weixin号第一次登录 }

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

PHP對稱加密函數實現資料的加密解密詳解

PHP影像辨識技術原理與實現

php  PATH_SEPARATOR判斷目前伺服器系統類型實例

以上是php實作微信掃碼自動登陸與註冊功能的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn