微信掃碼登陸

1 去微信開放平臺創(chuàng)建應用


審核通過后會得到AppID和AppSecret(這兩個很重要)

2 接入自己的網(wǎng)站


(1) 我用的是thinkphp框架,所以就用thinkphp給大家介紹

? ? ? ? ?創(chuàng)建WxController控制器

classWxLoginControllerextendsController{? ?

?? private$_appId;

? ? ? ?private$_appSecret;

? ? ? ?private$_webRoot;

? ? ? ?private$_openUrlQrc;

? ? ? ?private$_openUrlToken;

? ? ? ?private$_openUrlUserInfo;

? ? ? ?public function__construct()

? ? ? {

? ? ? ? ? ? ? $this->_appId='*********';//開放平臺網(wǎng)站應用

? ? ? ? ? ? ?$this->_appSecret='***********';

? ? ? ? ? ? ?$this->_webRoot='**********';//返回的域名網(wǎng)址,必須跟網(wǎng)站應用的域名網(wǎng)址相同

? ? ? ? ? ? $this->_openUrlQrc='https://open.weixin.qq.com/connect/qrconnect';//申請二維碼接口

? ? ? ? ? ? $this->_openUrlToken='https://api.weixin.qq.com/sns/oauth2/access_token';//申請token接口

? ? ? ? ? ? ?$this->_openUrlUserInfo='https://api.weixin.qq.com/sns/userinfo';//申請用戶信息接口

}

* 驗證微信掃碼后的用戶數(shù)據(jù)

*/

public functionwxCheck(){

$code=I('code');//只能使用1次即銷毀

$state=I('state');

if($state!=$_SESSION['wx_state']){

$this->error("請進入官網(wǎng)掃描二維碼",'/Home/WxLogin/wxLogin');

exit();

}

session('wx_state',null);//清除這個session

//獲取access_token和openid信息,還有用戶唯一標識unionid

$ken=$this->wxToken($code);//:access_token,expires_in,refresh_token,openid,scope,unionid

if($ken['errcode']==40029){

$this->error("code參數(shù)已經(jīng)過期");

}

//

//? ? ? ? //判斷是否已存在

$map=array(

'openid'=>$ken['openid'],

'unionid'=>$ken['unionid'],

);

$res=M('User')->field('user_id,user_name')->where(array('unionid'=>$map['unionid']))->find();//查詢openid是否存在,而PC和微信端 openid不一致,只有unionid才是唯一標識

if($res){

session('user_name',$res['user_name']);//寫入session

session('user_id',$res['user_id']);//寫入session

}else{

$user=$this->wxUserInfo($ken['access_token'],$ken['openid']);//獲取用戶信息

session('unionid',$user['unionid']);

session('nickname',$user['nickname']);

session('img',$user['headimgurl']);

session('type','微信');

$this->redirect('User/qqBind');

}

$this->redirect('Index/index');

}

/**

* 提交微信登錄請求

*/

public functionwxLogin(){

$stats=$this->getRandChar(16);//該參數(shù)可用于防止csrf攻擊(跨站請求偽造攻擊)

session('wx_state',$stats);//把隨機字符串寫入session,驗證時對比

$url=$this->_openUrlQrc.'?appid='.$this->_appId.'&redirect_uri='.urlencode($this->_webRoot).'&response_type=code&scope=snsapi_login&state='.$stats;

redirect($url);//跳轉到掃碼

}

//CURL獲取url返回值

functionhttpGet($url){

$oCurl=curl_init();//實例化

if(stripos($url,"https://")!==FALSE){

curl_setopt($oCurl,CURLOPT_SSL_VERIFYPEER,FALSE);

curl_setopt($oCurl,CURLOPT_SSL_VERIFYHOST,FALSE);

}

curl_setopt($oCurl,CURLOPT_URL,$url);

curl_setopt($oCurl,CURLOPT_RETURNTRANSFER,1);//是否返回值,1時給字符串,0輸出到屏幕

$sContent=curl_exec($oCurl);//獲得頁面數(shù)據(jù)

$aStatus=curl_getinfo($oCurl);//獲取CURL連接數(shù)據(jù)的信息

curl_close($oCurl);//關閉資源

//獲取成功

$output_array=json_decode($sContent,true);//轉換json格式

if(intval($aStatus["http_code"])==200){

return$output_array;

}else{

return false;

}

}

//獲取token信息

public functionwxToken($code){

$url=$this->_openUrlToken.'?appid='.$this->_appId.'&secret='.$this->_appSecret.'&code='.$code.'&grant_type=authorization_code';

$sContent=$this->httpGet($url);//獲取token數(shù)據(jù)

//? ? ? ? dump($sContent);

return$sContent;

}

//延長token時間,默認token兩個小時

public functionwxrefresh($refresh){

$url='https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$this->_appId.'&grant_type=refresh_token&refresh_token='.$refresh;

return$this->httpGet($url);

}

//檢驗token授權是否有效

public functionwxchecktoken($token,$openid){

$url='https://api.weixin.qq.com/sns/auth?access_token='.$token.'&openid='.$openid;

return$this->httpGet($url);

}

//獲取微信用戶個人信息,但公眾號和開放平臺opendid 會不一樣,unionid是用戶唯一標識

public functionwxUserInfo($token,$openid){

$url=$this->_openUrlUserInfo.'?access_token='.$token.'&lang=zh-CN&openid='.$openid;

return$this->httpGet($url);

}

}




最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容