Because of the popularity of domestic QQ users, so now the major sites are as far as possible to provide QQ landing, the following we look at the PHP version, for everyone to refer to the next
- /**
- * QQ Internet OAuth
- * @author Dyllen
- *
- */
- Class Oauth
- {
- Fetch Authorization Code URL
- Const PC_CODE_URL = ' https://graph.qq.com/oauth2.0/authorize ';
- Take Access Token URL
- Const PC_ACCESS_TOKEN_URL = ' Https://graph.qq.com/oauth2.0/token ';
- Fetch user Open Id URL
- Const OPEN_ID_URL = ' https://graph.qq.com/oauth2.0/me ';
- Callback Address after user authorization
- public $redirectUri = null;
- APP Id
- public $appid = null;
- APP Key
- public $appKey = null;
- Authorization list
- String, multiple separated by commas
- public $scope = null;
- Authorization Code
- public $code = null;
- Renew Credentials for access token
- public $refreshToken = null;
- Access token
- public $accessToken = null;
- Access token validity period, per second
- public $expiresIn = null;
- State
- public $state = null;
- public $openid = null;
- Construct
- Public function __construct ($config =[])
- {
- foreach ($config as $key = = $value) {
- $this $key = $value;
- }
- }
- /**
- * Get URL to get code
- * @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::P c_code_url. '?' . Http_build_query ($query);
- }
- /**
- * Take 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::P c_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;
- }
- /**
- * Refresh Access Tokens
- * @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::P c_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;
- }
- /**
- * Take user 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 Content
- * @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 method to fetch URL content
- * @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;
- }
- /**
- * Take State
- * @return String
- */
- protected function GetState ()
- {
- $this->state = MD5 (Uniqid (rand (), true));
- State is temporarily cached inside
- Define Yourself
- 。。。。。。。。。
- return $this->state;
- }
- /**
- * Verify State
- * @return Boolean
- */
- protected function Verifystate ()
- {
- 。。。。。。。
- }
- /**
- * Throws an exception
- * @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 ']);
- }
- /**
- * The OpenID is parsed from the return data obtained from the OpenID interface.
- * @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 '];
- }
- }
Copy CodeThe above mentioned is the whole content of this article, I hope you can like. |