PHP版QQ互聯(lián)OAuth示例代碼分享
更新時(shí)間:2015年07月05日 15:52:53 投稿:hebedich
這篇文章主要介紹了PHP版QQ互聯(lián)OAuth示例代碼分享,十分的詳細(xì)使用,有需要的小伙伴可以參考下。
由于國內(nèi)QQ用戶的普遍性,所以現(xiàn)在各大網(wǎng)站都盡可能的提供QQ登陸口,下面我們來看看php版,給大家參考下
/**
* QQ互聯(lián) oauth
* @author dyllen
*
*/
class Oauth
{
//取Authorization Code Url
const PC_CODE_URL = 'https://graph.qq.com/oauth2.0/authorize';
//取Access Token Url
const PC_ACCESS_TOKEN_URL = 'https://graph.qq.com/oauth2.0/token';
//取用戶 Open Id Url
const OPEN_ID_URL = 'https://graph.qq.com/oauth2.0/me';
//用戶授權(quán)之后的回調(diào)地址
public $redirectUri = null;
// App Id
public $appid = null;
//App Key
public $appKey = null;
//授權(quán)列表
//字符串,多個(gè)用逗號隔開
public $scope = null;
//授權(quán)code
public $code = null;
//續(xù)期access token的憑證
public $refreshToken = null;
//access token
public $accessToken = null;
//access token 有效期,單位秒
public $expiresIn = null;
//state
public $state = null;
public $openid = null;
//construct
public function __construct($config=[])
{
foreach($config as $key => $value) {
$this->$key = $value;
}
}
/**
* 得到獲取Code的url
* @throws \InvalidArgumentException
* @return string
*/
public function codeUrl()
{
if (!$this->redirectUri) {
throw new \Exception('parameter $redirectUri must be set.');
}
$query = [
'response_type' => 'code',
'client_id' => $this->appid,
'redirect_uri' => $this->redirectUri,
'state' => $this->getState(),
'scope' => $this->scope,
];
return self::PC_CODE_URL . '?' . http_build_query($query);
}
/**
* 取access token
* @throws Exception
* @return boolean
*/
public function getAccessToken()
{
$params = [
'grant_type' => 'authorization_code',
'client_id' => $this->appid,
'client_secret' => $this->appKey,
'code' => $this->code,
'redirect_uri' => $this->redirectUri,
];
$url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params);
$content = $this->getUrl($url);
parse_str($content, $res);
if ( !isset($res['access_token']) ) {
$this->thrwoError($content);
}
$this->accessToken = $res['access_token'];
$this->expiresIn = $res['expires_in'];
$this->refreshToken = $res['refresh_token'];
return true;
}
/**
* 刷新access token
* @throws Exception
* @return boolean
*/
public function refreshToken()
{
$params = [
'grant_type' => 'refresh_token',
'client_id' => $this->appid,
'client_secret' => $this->appKey,
'refresh_token' => $this->refreshToken,
];
$url = self::PC_ACCESS_TOKEN_URL . '?' . http_build_query($params);
$content = $this->getUrl($url);
parse_str($content, $res);
if ( !isset($res['access_token']) ) {
$this->thrwoError($content);
}
$this->accessToken = $res['access_token'];
$this->expiresIn = $res['expires_in'];
$this->refreshToken = $res['refresh_token'];
return true;
}
/**
* 取用戶open id
* @return string
*/
public function getOpenid()
{
$params = [
'access_token' => $this->accessToken,
];
$url = self::OPEN_ID_URL . '?' . http_build_query($params);
$this->openid = $this->parseOpenid( $this->getUrl($url) );
return $this->openid;
}
/**
* get方式取url內(nèi)容
* @param string $url
* @return mixed
*/
public function getUrl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* post方式取url內(nèi)容
* @param string $url
* @param array $keysArr
* @param number $flag
* @return mixed
*/
public function postUrl($url, $keysArr, $flag = 0)
{
$ch = curl_init();
if(! $flag) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $keysArr);
curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
}
/**
* 取state
* @return string
*/
protected function getState()
{
$this->state = md5(uniqid(rand(), true));
//state暫存在緩存里面
//自己定義
//。。。。。。。。。
return $this->state;
}
/**
* 驗(yàn)證state
* @return boolean
*/
protected function verifyState()
{
//。。。。。。。
}
/**
* 拋出異常
* @param string $error
* @throws \Exception
*/
protected function thrwoError($error)
{
$subError = substr($error, strpos($error, "{"));
$subError = strstr($subError, "}", true) . "}";
$error = json_decode($subError, true);
throw new \Exception($error['error_description'], (int)$error['error']);
}
/**
* 從獲取openid接口的返回?cái)?shù)據(jù)中解析出openid
* @param string $str
* @return string
*/
protected function parseOpenid($str)
{
$subStr = substr($str, strpos($str, "{"));
$subStr = strstr($subStr, "}", true) . "}";
$strArr = json_decode($subStr, true);
if(!isset($strArr['openid'])) {
$this->thrwoError($str);
}
return $strArr['openid'];
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- QQ登錄 PHP OAuth示例代碼
- PHP實(shí)現(xiàn)QQ快速登錄的方法
- PHP第三方登錄—QQ登錄實(shí)現(xiàn)方法
- thinkPHP5項(xiàng)目中實(shí)現(xiàn)QQ第三方登錄功能
- PHP實(shí)現(xiàn)QQ登錄實(shí)例代碼
- PHP模擬QQ登錄的方法
- PHP實(shí)現(xiàn)QQ登錄的開原理和實(shí)現(xiàn)過程
- PHP+jquery+CSS制作頭像登錄窗(仿QQ登陸)
- 淺談PHP接入(第三方登錄)QQ登錄 OAuth2.0 過程中遇到的坑
- PHP調(diào)用QQ互聯(lián)接口實(shí)現(xiàn)QQ登錄網(wǎng)站功能示例
相關(guān)文章
PHP去除數(shù)組中重復(fù)的元素并按鍵名排序函數(shù)
用php實(shí)現(xiàn)的去除數(shù)組中重復(fù)的函數(shù)2008-08-08
php+html優(yōu)化頁面顯示速度的方法小結(jié)
這篇文章主要為大家詳細(xì)介紹了php結(jié)合html優(yōu)化頁面顯示速度的一些常見方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
PHP讀取數(shù)據(jù)庫并按照中文名稱進(jìn)行排序?qū)崿F(xiàn)代碼
有時(shí)候我們讀取數(shù)據(jù)庫輸出的時(shí)候可能會(huì)需要按照中文用戶名的方式進(jìn)行排序,有些新手朋友對此事無從下手,接下來由小編為您詳細(xì)介紹實(shí)現(xiàn)方法,感興趣的朋友可以了解下啊2013-01-01
無法載入 mcrypt 擴(kuò)展,請檢查 PHP 配置終極解決方案
今天運(yùn)行phpmyadmin的時(shí)候,提示無法載入 mcrypt 擴(kuò)展,經(jīng)排查原來是php配置問題。2011-07-07

