php QQ第三方登入/OAuth2.0驗證
阿新 • • 發佈:2018-12-31
qq實現第三方網站登入
建立QQ互聯賬號
可用QQ號碼登入 登入地址:http://connect.qq.com/
QQ互聯->管理中心->建立應用
1.建立應用
2.建立網站
注意內容:網站地址一定要加入圖中所標註的資訊
建立成功
獲取 APP ID 和 APP KEY
header請求
[php] view plain copy print?- publicfunction actionQQ()
- {
- //$redirect 為回撥地址 $app_id 應用編號
- $url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id='
- header('Location:' . $url);
- }
public function actionQQ() { //$redirect 為回撥地址 $app_id 應用編號 $url = 'https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=' . $app_id . '&redirect_uri=' . $redirect; header('Location:' . $url); }
獲取QQ使用者資訊 [php] view plain copy print?
- <?php
- class QQ_sdk
- {
- private$app_id = '101****0572';
- private$app_secret = 'e55264******132366';
- private$redirect = 'http://www.***.cn/';
- function __construct()
- {
- }
- /**
- * [get_open_id 獲取使用者唯一ID,openid]
- * @param [string] $token [授權碼]
- * @return [array] [成功返回client_id 和 openid ;失敗返回error 和 error_msg]
- */
- function get_open_id($token)
- {
- $str = $this->curl_get_content('https://graph.qq.com/oauth2.0/me?access_token=' . $token);
- if (strpos($str, "callback") !== false) {
- $lpos = strpos($str, "(");
- $rpos = strrpos($str, ")");
- $str = substr($str, $lpos + 1, $rpos - $lpos - 1);
- }
- $user = json_decode($str, TRUE);
- return$user;
- }
- /**
- * [get_access_token 獲取access_token]
- * @param [string] $code [登陸後返回的$_GET['code']]
- * @return [array] [expires_in 為有效時間 , access_token 為授權碼 ; 失敗返回 error , error_description ]
- */
- function get_access_token($code)
- {
- $token_url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&'
- . 'client_id=' . $this->app_id . '&redirect_uri=' . urlencode($this->redirect) . '&client_secret=' . $this->app_secret . '&code=' . $code;
- $token = array();
- parse_str($this->curl_get_content($token_url), $token);
- return$token;
- }
- /**
- * [get_user_info 獲取使用者資訊]
- * @param [string] $token [授權碼]
- * @param [string] $open_id [使用者唯一ID]
- * @return [array] [ret:返回碼,為0時成功。msg為錯誤資訊,正確返回時為空。...params]
- */
- function get_user_info($token, $open_id)
- {
- $user_info_url = 'https://graph.qq.com/user/get_user_info?' . 'access_token=' . $token . '&oauth_consumer_key=' . $this->app_id . '&openid=' . $open_id . '&format=json';
- $info = json_decode($this->curl_get_content($user_info_url), TRUE);
- return$info;
- }
- privatefunction curl_get_content($url)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_URL, $url);
- //設定超時時間為3s
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
- $result = curl_exec($ch);
- curl_close($ch);
- return$result;
- }
- }
<?php
class QQ_sdk
{
private $app_id = '101****0572';
private $app_secret = 'e55264******132366';
private $redirect = 'http://www.***.cn/';
function __construct()
{
}
/**
* [get_open_id 獲取使用者唯一ID,openid]
* @param [string] $token [授權碼]
* @return [array] [成功返回client_id 和 openid ;失敗返回error 和 error_msg]
*/
function get_open_id($token)
{
$str = $this->curl_get_content('https://graph.qq.com/oauth2.0/me?access_token=' . $token);
if (strpos($str, "callback") !== false) {
$lpos = strpos($str, "(");
$rpos = strrpos($str, ")");
$str = substr($str, $lpos + 1, $rpos - $lpos - 1);
}
$user = json_decode($str, TRUE);
return $user;
}
/**
* [get_access_token 獲取access_token]
* @param [string] $code [登陸後返回的$_GET['code']]
* @return [array] [expires_in 為有效時間 , access_token 為授權碼 ; 失敗返回 error , error_description ]
*/
function get_access_token($code)
{
$token_url = 'https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&'
. 'client_id=' . $this->app_id . '&redirect_uri=' . urlencode($this->redirect) . '&client_secret=' . $this->app_secret . '&code=' . $code;
$token = array();
parse_str($this->curl_get_content($token_url), $token);
return $token;
}
/**
* [get_user_info 獲取使用者資訊]
* @param [string] $token [授權碼]
* @param [string] $open_id [使用者唯一ID]
* @return [array] [ret:返回碼,為0時成功。msg為錯誤資訊,正確返回時為空。...params]
*/
function get_user_info($token, $open_id)
{
$user_info_url = 'https://graph.qq.com/user/get_user_info?' . 'access_token=' . $token . '&oauth_consumer_key=' . $this->app_id . '&openid=' . $open_id . '&format=json';
$info = json_decode($this->curl_get_content($user_info_url), TRUE);
return $info;
}
private function curl_get_content($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
//設定超時時間為3s
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
可在設定<meta property="qc:admins" content="756420************1316367" />目錄下測試
[php] view plain copy print?- if(isset($_GET['code'])) {
- //qq獲取資訊
- include('QQ_sdk.php');
- $qq_sdk = new Qq_sdk();
- $token = $qq_sdk->get_access_token($_GET['code']);
- echo"<pre>";
- print_r($token)."</br>";
- echo"</pre>";
- $open_id = $qq_sdk->get_open_id($token['access_token']);
- echo"<pre>";
- print_r($open_id)."</br>";
- echo"</pre>";
- }
- ?>
if(isset($_GET['code'])) {
//qq獲取資訊
include('QQ_sdk.php');
$qq_sdk = new Qq_sdk();
$token = $qq_sdk->get_access_token($_GET['code']);
echo "<pre>";
print_r($token)."</br>";
echo "</pre>";
$open_id = $qq_sdk->get_open_id($token['access_token']);
echo "<pre>";
print_r($open_id)."</br>";
echo "</pre>";
}
?>