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

Source: Internet
Author: User
Tags oauth openid

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: // obtain the 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. Multiple authorization codes are 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 function _ construct ($ config = []) {foreach ($ config as $ key = >$ value) {$ this-> $ key = $ value ;}}/*** get 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.

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.