PHP調(diào)用QQ互聯(lián)接口實現(xiàn)QQ登錄網(wǎng)站功能示例
本文實例講述了PHP調(diào)用QQ互聯(lián)接口實現(xiàn)QQ登錄網(wǎng)站功能。分享給大家供大家參考,具體如下:
調(diào)用QQ登錄接口,首先要到QQ互聯(lián)完善開發(fā)者認證信息,并通過審核,然后創(chuàng)建一個網(wǎng)站應(yīng)用,獲得APP ID和APP Key,通過審核后即可調(diào)用基本接口get_user_info(獲得用戶信息),實現(xiàn)QQ登錄網(wǎng)站功能。
廢話不多,上示例代碼(QQ登錄李維山博客):
<?php header("Content-Type: text/html;charset=utf-8"); //應(yīng)用APP ID $app_id = "101486017"; //應(yīng)用APP Key $app_secret = "13a1811780f29d7a5b64e598c38a4494"; //應(yīng)用填寫的網(wǎng)站回調(diào)域 $my_url = "http://www.msllws.top/qqlogin"; //Step1:獲取Authorization Code session_start(); $code = $_REQUEST["code"];//存放Authorization Code 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){ // ...... echo("<script> top.location.); }else{ echo '未知錯誤'; } }else{ echo("The state does not match. You may be a victim of CSRF."); }
登錄效果:
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php curl用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》及《PHP中json格式數(shù)據(jù)操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
原生JS實現(xiàn)Ajax通過POST方式與PHP進行交互的方法示例
這篇文章主要介紹了原生JS實現(xiàn)Ajax通過POST方式與PHP進行交互的方法,涉及ajax使用post方式與后臺交互及php數(shù)據(jù)接收、處理、查詢數(shù)據(jù)庫等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05淺談PHP的exec()函數(shù)無返回值排查方法(必看)
下面小編就為大家?guī)硪黄獪\談PHP的exec()函數(shù)無返回值排查方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03PHP使用mongoclient簡單操作mongodb數(shù)據(jù)庫示例
這篇文章主要介紹了PHP使用mongoclient簡單操作mongodb數(shù)據(jù)庫,結(jié)合實例形式分析了php使用mongoclient針對MongoDB數(shù)據(jù)庫的連接、增刪改查及相關(guān)函數(shù)使用技巧,需要的朋友可以參考下2019-02-02php基于Fleaphp框架實現(xiàn)cvs數(shù)據(jù)導(dǎo)入MySQL的方法
這篇文章主要介紹了php基于Fleaphp框架實現(xiàn)cvs數(shù)據(jù)導(dǎo)入MySQL的方法,涉及PHP基于Fleaphp框架針對cvs文件的讀取及數(shù)據(jù)庫操作相關(guān)技巧,需要的朋友可以參考下2016-02-02