《PHP學習:php實現微信掃碼自動登陸與注冊功能》要點:
本文介紹了PHP學習:php實現微信掃碼自動登陸與注冊功能,希望對您有用。如果有疑問,可以聯系我們。
PHP應用本文實例講述了php實現微信掃碼自動登陸與注冊功能.分享給大家供大家參考,具體如下:
PHP應用微信開發已經是現在程序員必須要掌握的一項基本的技術了,其實做過微信開發的都知道微信接口非常的強大做起來也非常的簡單,這里我們一起來看一個微信自動登陸注冊的例子.
PHP應用php 微信掃碼 pc端自動登陸注冊 用的接口scope 是snsapi_userinfo,微信登陸一個是網頁授權登陸,另一個是微信聯合登陸
PHP應用網頁授權登陸:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
PHP應用微信聯合登陸:https://open.weixin.qq.com/cgi-bin/frame?t=home/web_tmpl&lang=zh_CN
PHP應用一、首先把微信鏈接帶個標識生成二維碼
PHP應用比如鏈接為 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你傳入什么微信那邊返回什么
PHP應用可以作為服務器與微信段的一個標識:
PHP應用
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);
}
}
PHP應用此時生成了二維碼 state是標識,phpqrcode可以在文章末尾下載,這樣我們設置了回調地址http://www.xxx.net/login/wcallback
PHP應用就可以在wcallback方法里面處理數據 插入用戶 生成session,跳轉登陸,pc端可以設置幾秒鐘ajax請求服務器,一旦獲取到了state,即實現調整,微信瀏覽器里處理完后可以關閉窗口,微信js可實現:
PHP應用
document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
WeixinJSBridge.call('closeWindow');}, false);
PHP應用也可以授權登陸成功后跳轉到微信服務號關注頁面:
PHP應用
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 ''
PHP應用phpqrcode類庫下載在此不提供各位可以百度搜索下載
PHP應用magento微信掃碼網站自動登錄的例子
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&lang=zh_CN
PHP應用查看授權后接口調用(UnionID),不難發現填寫回調地址,用戶確認登陸pc端即可跳轉
PHP應用獲取UnionID方法
PHP應用
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 '<h1>錯誤:</h1>'.$token->errcode;
echo '<br/><h2>錯誤信息:</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;
//轉成對象
$access_token = json_decode($this->https_request($access_token_url));
if (isset($access_token->errcode)) {
echo '<h1>錯誤:</h1>'.$access_token->errcode;
echo '<br/><h2>錯誤信息:</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';
//轉成對象
$user_info = json_decode($this->https_request($user_info_url));
if (isset($user_info->errcode)) {
echo '<h1>錯誤:</h1>'.$user_info->errcode;
echo '<br/><h2>錯誤信息:</h2>'.$user_info->errmsg;
exit;
}
//打印用戶信息
// echo '<pre>';
// print_r($user_info);
// echo '</pre>';
//獲取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微信開發技巧匯總》、《PHP編碼與轉碼操作技巧匯總》、《PHP網絡編程技巧總結》、《PHP基本語法入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》
PHP應用希望本文所述對大家PHP程序設計有所幫助.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/3770.html