網(wǎng)站應用接入qq登陸

想使用

話不多說,直接上代碼

第一步 單擊登錄按鈕進行頁面跳轉(zhuǎn)(需要修改/oauth/qq/index.php這個鏈接,換成自己的授權(quán)的頁面)

<?php

session_start();

if($_SESSION['qqInfo'])

{

? ? echo '<pre>';

? ? print_r($_SESSION['qqInfo']);

}else{

? ? $str = '

? ? <html lang="zh-cn">

? ? ? ? <head>

? ? ? ? ? ? <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

? ? ? ? ? ? <title>QQ授權(quán)</title>

? ? ? ? ? ? <script type="text/javascript">

? ? ? ? ? ? ? ? var childWindow;

? ? ? ? ? ? ? ? function toQzoneLogin()

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? childWindow = window.open("/oauth/qq/index.php","TencentLogin","width=450,height=320,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? function closeChildWindow()

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? childWindow.close();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? </script>

? ? ? ? </head>

? ? ? ? <body>? ? ?

? ? ? ? ? ? <a href="#" onclick="toQzoneLogin()"><img src="/img/qq_login.png"></a>

? ? ? ? </body>

? ? </html>';

? ? echo $str;

? ? unset($str);

}

?>


第二步? 獲取qq接口的全七八糟的東西? 在你第一步跳轉(zhuǎn)的頁面的文件里面

我是在/oauth/qq/index.php這里面

<?php

? ? header("Content-Type: text/html;charset=utf-8");

? ? //應用APP ID

? ? $app_id = "";

? ? //應用APP Key

? ? $app_secret = "";

? ? //應用填寫的網(wǎng)站回調(diào)域,就是你創(chuàng)建應用是下面填寫的回調(diào)地址

? ? $my_url = "";


? ? //Step1:獲取Authorization Code

? ? session_start();//這個不用說吧,開啟session

? ? $code = $_REQUEST["code"];//獲取Authorization Code

//code不存在時,前往qq接口獲取

if(empty($code)) {

? ? ? ? //state參數(shù)用于防止CSRF攻擊,成功授權(quán)后回調(diào)時原樣帶回

? ? ? ? $_SESSION['state'] = md5(uniqid(rand(), TRUE));

? ? ? ? //拼接URL

? ? ? ? $dialog_url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&state=".$_SESSION['state'];

? ? ? ? echo("<script> top.location.href='".$dialog_url."'</script>");

? ? }


? ? //Step2:通過Authorization Code獲取Access Token

? ? if($_REQUEST['state'] == $_SESSION['state'] || 1) {

? ? ? ? //拼接URL

? ? ? ? $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&"."client_id=".$app_id."&redirect_uri=".urlencode($my_url)."&client_secret=".$app_secret."&code=".$code;

? ? ? ? $response = file_get_contents($token_url);

? ? ? ? //如果用戶臨時改變主意取消登錄,返回true!==false,否則執(zhí)行step3?

? ? ? ? if (strpos($response, "callback") !== false) {

? ? ? ? ? ? $lpos = strpos($response, "(");

? ? ? ? ? ? $rpos = strrpos($response, ")");

? ? ? ? ? ? $response = substr($response, $lpos + 1, $rpos - $lpos -1);

? ? ? ? ? ? $msg = json_decode($response);

? ? ? ? ? ? if (isset($msg->error)) {

? ? ? ? ? ? ? ? echo "<h3>error:</h3>".$msg->error;

? ? ? ? ? ? ? ? echo "<h3>msg :</h3>".$msg->error_description;

? ? ? ? ? ? ? ? exit;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? //Step3:使用Access Token來獲取用戶的OpenID

? ? ? ? $params = array();

? ? ? ? parse_str($response, $params);//把傳回來的數(shù)據(jù)參數(shù)變量化

? ? ? ? $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];

? ? ? ? $str = file_get_contents($graph_url);

? ? ? ? if (strpos($str, "callback") !== false) {

? ? ? ? ? ? $lpos = strpos($str, "(");

? ? ? ? ? ? $rpos = strrpos($str, ")");

? ? ? ? ? ? $str = substr($str, $lpos + 1, $rpos - $lpos -1);

? ? ? ? }

? ? ? ? $user = json_decode($str);//存放返回的數(shù)據(jù) client_id ,openid

? ? ? ? if (isset($user->error)) {

? ? ? ? ? ? echo "<h3>error:</h3>".$user->error;

? ? ? ? ? ? echo "<h3>msg :</h3>".$user->error_description;

? ? ? ? ? ? exit;

? ? ? ? }


? ? ? ? //Step4:使用openid和access_token獲取用戶信息

? ? ? ? $user_data_url = "https://graph.qq.com/user/get_user_info?access_token={$params['access_token']}&oauth_consumer_key={$app_id}&openid={$user->openid}&format=json";

? ? ? ? $user_data = file_get_contents($user_data_url);//獲取到的用戶信息

? ? ? ? //以下為授權(quán)成功后的自定義操作

? ? ? ? if($user_data){

? ? ? ? ? ? $_SESSION['qqInfo'] = $user_data;

? ? ? ? ? ? //獲取登陸用戶信息成功過后需要跳轉(zhuǎn)的頁面,填寫你第一步的頁面地址

? ? ? ? ? ? echo("<script> top.location.href=' '</script>");

? ? ? ? }else{

? ? ? ? ? ? echo '未知錯誤';

? ? ? ? }

? ? }else{

? ? ? ? echo("The state does not match. You may be a victim of CSRF.");

? ? }


第三步? 去你的回調(diào)頁面接收code

我的回調(diào)頁面是在/oauth/qq/callback.php

<?php

//接受code

$code = $_REQUEST["code"];

//接受state

$state = $_REQUEST["state"];

//得到code 與 state 跳轉(zhuǎn)

//跳轉(zhuǎn)地址,這里是跳回你的第二步的頁面,寫全路徑

$url = "***/oauth/qq/index.php?code=".$code."&state=".$state;

echo("<script> top.location.href='".$url."'</script>");

?>


如果頁面能打印到你的信息了就說明成功了,

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容