QQ interconnection OAuth-PHP source code

Source: Internet
Author: User
Tags oauth openid
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'];}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.