0
Code snippet
(1)
[View all codes in full screen]
1.
[Code][PHP] code
Jump to [1] [full screen preview]
/*** QQ internet oauth * @ author dyllen **/class Oauth {// obtain Authorization Code Urlconst PC_CODE_URL =' https://graph.qq.com/oauth2.0/authorize '; // Obtain Access Token Urlconst PC_ACCESS_TOKEN_URL =' https://graph.qq.com/oauth2.0/token '; // Obtain the user's Open Id Urlconst OPEN_ID_URL =' https://graph.qq.com/oauth2.0/me '; // The User-authorized callback address public $ redirectUri = null; // App Idpublic $ appid = null; // App Keypublic $ appKey = null; // authorization list // string, separated by commas (,). public $ scope = null; // codepublic $ code = null; // public $ refreshToken = null; // access tokenpublic $ accessToken = null; // access token validity period, in seconds public $ expiresIn = null; // statepublic $ state = null; public $ openid = null; // constructpublic function _ construct ($ confi G = []) {foreach ($ config as $ key => $ value) {$ this-> $ key = $ value ;}} /*** get the 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 _ Uris '=> $ this-> redirectUri, 'status' => $ 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 ;} /*** refresh 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;}/*** get 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, success, TRUE); curl_setopt ($ ch, CURLOPT_URL, $ url); $ response = curl_exec ($ ch ); curl_close ($ ch); return $ respo Neuron ;} /*** get url content in post mode * @ 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, latency, FALSE); curl_setopt ($ ch, latency, 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 ;} /*** get state * @ return string */protected function getState () {$ this-> state = md5 (uniqid (rand (), true )); // state is saved in the cache. // You can customize the state //. ........ Return $ this-> state;}/*** verify state * @ return boolean */protected function verifyState (){//.......} /*** Throw 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']);}/*** parse the openid * @ param string $ str * @ return string */protected function parseOp from the returned data of the obtained openid interface Enid ($ 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'];}