PHP QQ Third party login SDK program code

Source: Internet
Author: User


The main consideration is to QQ PHP SDK write is really rotten, is a universal API knowledge, rather than hand can deploy class library. Anyway, I wrote one, and I took it out to share.

Nothing more to say, directly on the code.

qq_sdk.php

The code is as follows Copy Code

<?php

/**

* QQ Development Platform SDK

* Author: Occasionally intoxicated

* blog:www.stutostu.com

*/

Class qq_sdk{

Configure app Parameters

Private $app _id = your app id;

Private $app _secret = ' Your App_secret ';

Private $redirect = your callback address;

function __construct ()

{

}

/**

* [Get_access_token get Access_token]

* @param [string] $code [$_get[' code ' returned after landing]]

* @return [Array] [expires_in is a valid time, Access_token is the authorization code; failure returns error, Error_description]

*/

function Get_access_token ($code)

{

Get Access_token

$token _url = ' https://graph.qq.com/oauth2.0/token?grant_type=authorization_code& '

. ' Client_id= '. $this->app_id. ' &redirect_uri= '. UrlEncode ($this->redirect)/callback Address

. ' &client_secret= '. $this->app_secret. ' &code= '. $code;

$token = Array ();

Expires_in for Access_token active time increment

Parse_str ($this->_curl_get_content ($token _url), $token);

return $token;

}

/**

* [get_open_id get user only Id,openid]

* @param [string] $token [Authorization code]

* @return [Array] [successfully returned client_id and OpenID; failure returns error and ERROR_MSG]

*/

function get_open_id ($token)

{

$str = $this->_curl_get_content (' https://graph.qq.com/oauth2.0/me?access_token= '. $token);

if (Strpos ($str, "callback")!== false)

{

$lpos = Strpos ($str, "(");

$rpos = Strrpos ($str, ")");

$str = substr ($str, $lpos + 1, $rpos – $lpos-1);

}

$user = Json_decode ($str, TRUE);

return $user;

}

/**

* [Get_user_info get user Information]

* @param [string] $token [Authorization code]

* @param [string] $open _id [user unique ID]

* @return [array] [RET: Return code, successful at 0 o'clock. MSG is an error message and is empty when correctly returned. ... params]

*/

function Get_user_info ($token, $open _id)

{

Assembly URL

$user _info_url = ' Https://graph.qq.com/user/get_use
R_info? '

. ' Access_token= '. $token

. ' &oauth_consumer_key= '. $this->app_id

. ' &openid= '. $open _id

. ' &format=json ';

$info = Json_decode ($this->_curl_get_content ($user _info_url), TRUE);

return $info;

}

Private Function _curl_get_content ($url)

{

$ch = Curl_init ();

curl_setopt ($ch, Curlopt_returntransfer, TRUE);

curl_setopt ($ch, Curlopt_url, $url);

Set timeout time to 3s

curl_setopt ($ch, Curlopt_connecttimeout, 3);

$result = curl_exec ($ch);

Curl_close ($ch);

return $result;

}

}

/* End of qq_sdk.php * *

How to use: Place hyperlinks on your site, address: https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id= your app_id& Redirect_uri= your callback address.

Call me above this QQ_SDK on the callback address.

Demo as follows:

  code is as follows copy code

 

if (Empty ($_get[' code '))  

Exit (' parameter illegal ');  

 

Include (' qq_sdk ');  

$qq _sdk = new Qq_sdk ()  

$token = $qq _sdk->get_access_token ($_get[' Code '];  

Print_r ($token);  

 

$open _id = $qq _sdk->get_open_id ($token [' Access _token ']);  

Print_r ($open _id);  

 

 

$user _info = $qq _sdk->get_ User_info ($token [' Access_token '], $open _id[' OpenID ']);  

Print_r ($user _info);  

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.