由於國內QQ用戶的普遍性,所以現在各大網站都盡可能的提供QQ登陸口,下面我們來看看php版,給大家參考下
-
- /**
- * QQ互聯 oauth
- * @author dyllen
- *
- */
- class Oauth
- {
- //取Authorization Code Url
- const PC_COhttps_URL = '取Authorization Code Url
- const PC_COhttps_URL = '取AuthorizationL = ' //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';
-
- //使用者授權之後的回呼位址
- public $redirectUri = null;
-
- // App Id
- public $appid = null;
-
- //App Key
- public $appKey = null;
-
- //授權清單
- //字串,多個用逗號隔開
- public $scope = null;
-
- //授權code
- public $code = null ;
-
- //續期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) {
- {
- 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);
- }
-
- /**
- * 取得存取權杖
- * @throws Exception
- * @return boolean
- */
- publicunc.
- {
- $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['access_token'];
- $this->expiresIn = $res['access_token'];
- $this->expiresIn = $res['access_token'];
- $this->expiresIn = $res['access_token'];
- $this->expiresIn = $res['access_token'];
- $this->expiresIn = $res['access_token'];
- $this->expiresIn = $res['access_token'];
- $this->expiresIn = $res[’access_token'];
- $this->expiresIn = $res['expires_in'];
- $this->refreshToken = $res['refresh_token'];
-
- return true;
- }
-
- /**
- * 刷新存取權杖
- * @throws Exception
- * @return boolean
- */
- public function refreshToken()
- {
- $params = [
- 'grant_type' => 'refresh_token',
- 'client_id' => $this->appid,
-
- 'client_id' => $this->appid,
-
- 'client_secret' => $this->appKey,
- 'refresh_token' => $this->refreshToken,
- ];
-
- $url = self::PC_ACCESS_TOKEN_URL . '🎜> $url = self::PC_ACCESS_TOKEN_URL . '🎜> $url = self::PC_ACCESS_TOKEN_URL . '🎜 . ' $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;
- }
-
- /** public function getOpenid()
- {
- $params = [
- 'access_token' => $this->accessToken,
- ];
-
- $url = self::🎜> ];
-
- $url = self::OPEN_ID_URL. '?' . http_build_query($params);
-
- $this->openid = $this->parseOpenid( $this->getUrl($url) );
-
- return $this-> openid;
- }
- /** * 取使用者open id * @return string*/ 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); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url); $c. ($ch); return $response; }
-
- /**
- * 發佈 URL 內容
- * @param string $url
- * @param array $keysArr
- * @param number $flag
- * @return mix
- * @param number $flag
- * @return mix
- */
- 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 $
- curl_close($ch);
- ret ;
- }
-
-
- /**
- * 取得狀態
- * @return string
- */
- 受保護函數getState()
- {
- $this->state = md5(uniqid(rand() ) , true));
- //state暫存在儲存裡面
- //自己定義
- //。
- }
-
- /**
- * 驗證state
- * @return boolean
- */
- protected function verifyState()
- {
- //。 🎜> /**
- * 拋出例外
- * @param string $error
- * @throws Exception
- */
- 受保護函數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介面的回傳資料解析出openid
- * @param string $str
- * @return string
- */
- 受保護函數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'];
- }
- }
-
-
複製代碼
以上說的就是這篇文章的全部內容了,希望大家能夠喜歡。
互聯,PHP
|