PHP version QQ interconnection OAuth sample code sharing, oauth sample code _ PHP Tutorial

Source: Internet
Author: User
PHP version QQ internet OAuth sample code sharing, oauth sample code. The PHP version of QQ internet OAuth sample code is shared. the oauth sample code is widely used by QQ users in China, so all major websites are providing QQ login ports as much as possible. let's take a look at the php version, PHP version QQ interconnection OAuth sample code sharing, oauth Sample code

Due to the universality of QQ users in China, all major websites now provide QQ login ports as much as possible. let's take a look at the php version for your reference.

/*** QQ internet oauth * @ author dyllen **/class Oauth {// obtain Authorization Code Url const PC_CODE_URL =' https://graph.qq.com/oauth2.0/authorize '; // Get Access Token Url const PC_ACCESS_TOKEN_URL =' https://graph.qq.com/oauth2.0/token '; // Obtain the user's Open Id Url const OPEN_ID_URL =' https://graph.qq.com/oauth2.0/me '; // The User-authorized callback address public $ redirectUri = null; // App Id public $ appid = null; // App Key public $ appKey = null; // authorization list // string, separated by commas (,). public $ scope = null; // authorization code public $ code = null; // public $ refreshToken = null; // access token public $ accessToken = null; // access token validity period, in seconds public $ expiresIn = null; // state public $ state = null; public $ openid = null; // construct public funct Ion _ construct ($ config = []) {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-> PpKey, '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 $ response ;} /*** 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 temporarily saved Save/and define it by yourself //......... 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 funct from the returned data of the obtained openid interface Ion 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'];}

The above is all the content of this article. I hope you will like it.

Thanks to the universality of QQ users in China, all major websites now provide QQ login ports as much as possible. let's take a look at the php version ,...

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.