php實(shí)現(xiàn)微信掃碼自動(dòng)登陸與注冊(cè)功能
本文實(shí)例講述了php實(shí)現(xiàn)微信掃碼自動(dòng)登陸與注冊(cè)功能。分享給大家供大家參考,具體如下:
微信開(kāi)發(fā)已經(jīng)是現(xiàn)在程序員必須要掌握的一項(xiàng)基本的技術(shù)了,其實(shí)做過(guò)微信開(kāi)發(fā)的都知道微信接口非常的強(qiáng)大做起來(lái)也非常的簡(jiǎn)單,這里我們一起來(lái)看一個(gè)微信自動(dòng)登陸注冊(cè)的例子.
php 微信掃碼 pc端自動(dòng)登陸注冊(cè) 用的接口scope 是snsapi_userinfo,微信登陸一個(gè)是網(wǎng)頁(yè)授權(quán)登陸,另一個(gè)是微信聯(lián)合登陸
網(wǎng)頁(yè)授權(quán)登陸:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
微信聯(lián)合登陸:https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN
一、首先把微信鏈接帶個(gè)標(biāo)識(shí)生成二維碼
比如鏈接為 https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$url.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect' 我們可以在state上做文章,因?yàn)閟tate你傳入什么微信那邊返回什么
可以作為服務(wù)器與微信段的一個(gè)標(biāo)識(shí):
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);
}
}
此時(shí)生成了二維碼 state是標(biāo)識(shí),phpqrcode可以在文章末尾下載,這樣我們?cè)O(shè)置了回調(diào)地址http://www.xxx.net/login/wcallback
就可以在wcallback方法里面處理數(shù)據(jù) 插入用戶(hù) 生成session,跳轉(zhuǎn)登陸,pc端可以設(shè)置幾秒鐘ajax請(qǐng)求服務(wù)器,一旦獲取到了state,即實(shí)現(xiàn)調(diào)整,微信瀏覽器里處理完后可以關(guān)閉窗口,微信js可實(shí)現(xiàn):
document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
WeixinJSBridge.call('closeWindow');}, false);
也可以授權(quán)登陸成功后跳轉(zhuǎn)到微信服務(wù)號(hào)關(guān)注頁(yè)面:
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('授權(quán)失敗');
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 '錯(cuò)誤:'.$token->errcode;
echo '錯(cuò)誤信息:'.$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;
//轉(zhuǎn)成對(duì)象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo '錯(cuò)誤:'.$access_token->errcode;
echo '錯(cuò)誤信息:'.$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'
//轉(zhuǎn)成對(duì)象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo '錯(cuò)誤:'.$user_info->errcode;
echo '錯(cuò)誤信息:'.$user_info->errmsg;
exit;
}
//打印用戶(hù)信息
// echo ''
// print_r($user_info);
// echo ''
phpqrcode類(lèi)庫(kù)下載在此不提供各位可以百度搜索下載
magento微信掃碼網(wǎng)站自動(dòng)登錄的例子
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN
查看授權(quán)后接口調(diào)用(UnionID),不難發(fā)現(xiàn)填寫(xiě)回調(diào)地址,用戶(hù)確認(rèn)登陸pc端即可跳轉(zhuǎn)
獲取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('授權(quán)失敗');
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 '<h1>錯(cuò)誤:</h1>'.$token->errcode;
echo '<br/><h2>錯(cuò)誤信息:</h2>'.$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;
//轉(zhuǎn)成對(duì)象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo '<h1>錯(cuò)誤:</h1>'.$access_token->errcode;
echo '<br/><h2>錯(cuò)誤信息:</h2>'.$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';
//轉(zhuǎn)成對(duì)象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo '<h1>錯(cuò)誤:</h1>'.$user_info->errcode;
echo '<br/><h2>錯(cuò)誤信息:</h2>'.$user_info->errmsg;
exit;
}
//打印用戶(hù)信息
// echo '<pre>';
// print_r($user_info);
// echo '</pre>';
//獲取unionid
$uid=$user_info->unionid;
}
//用戶(hù)操作處理 分為再次登錄和第一次登陸
$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號(hào)再次登錄
}{//該weixin號(hào)第一次登錄
}
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《PHP微信開(kāi)發(fā)技巧匯總》、《PHP編碼與轉(zhuǎn)碼操作技巧匯總》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php實(shí)現(xiàn)cc攻擊防御和防止快速刷新頁(yè)面示例
這篇文章主要介紹了php實(shí)現(xiàn)cc攻擊防御的方法和防止快速刷新頁(yè)面示例,需要的朋友可以參考下2014-02-02
Yii框架使用魔術(shù)方法實(shí)現(xiàn)跨文件調(diào)用功能示例
這篇文章主要介紹了Yii框架使用魔術(shù)方法實(shí)現(xiàn)跨文件調(diào)用功能,涉及Yii框架中php面向?qū)ο蟪绦蛟O(shè)計(jì)相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
CentOS 安裝 PHP5.5+Redis+XDebug+Nginx+MySQL全紀(jì)錄
這篇文章主要介紹了在CentOS系統(tǒng)環(huán)境下安裝 PHP5.5+Redis+XDebug+Nginx+MySQL開(kāi)發(fā)環(huán)境的全過(guò)程,非常的細(xì)致詳盡,推薦給有需要的小伙伴們參考下吧。2015-03-03
destoon供應(yīng)信息title調(diào)用出公司名稱(chēng)的方法
這篇文章主要介紹了destoon供應(yīng)信息title調(diào)用出公司名稱(chēng)的方法,非常具有實(shí)用價(jià)值的一個(gè)技巧,需要的朋友可以參考下2014-08-08
在Mac OS上搭建Nginx+PHP+MySQL開(kāi)發(fā)環(huán)境的教程
這篇文章主要介紹了在Mac OS上安裝配置Nginx+PHP+MySQL開(kāi)發(fā)環(huán)境的教程,雖然Mac自帶PHP,但還是要注意一下php-fpm報(bào)錯(cuò)問(wèn)題的發(fā)生,需要的朋友可以參考下2015-12-12
laravel5.1框架下的批量賦值實(shí)現(xiàn)方法分析
這篇文章主要介紹了laravel5.1框架下的批量賦值實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Laravel5.1框架批量賦值的相關(guān)原理與實(shí)現(xiàn)方法,需要的朋友可以參考下2019-12-12
利用php的ob緩存機(jī)制實(shí)現(xiàn)頁(yè)面靜態(tài)化方法
下面小編就為大家?guī)?lái)一篇利用php的ob緩存機(jī)制實(shí)現(xiàn)頁(yè)面靜態(tài)化方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07

