QQ Login PHP OAuth Sample Code _php instance

Source: Internet
Author: User
Tags curl error code hmac oauth openid sha1 urlencode
Written according to official documents
Copy Code code as follows:

<?php
/**
* Apply for Http://connect.opensns.qq.com/apply
* List http://connect.opensns.qq.com/my
*/
Session_Start ();
$QQ _oauth_config = Array (
' Oauth_consumer_key ' => ' ******* ',//app ID
' Oauth_consumer_secret ' => ' ****************** ',//app KEY
' Oauth_callback ' => "Http://www.955.cc/qq.php?action=reg",//modified here as the current script, but want to keep? Action=reg
' Oauth_request_token_url ' => ' Http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token ',
' Oauth_authorize_url ' => ' http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize ',
' Oauth_request_access_token_url ' => ' Http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token ',
' User_info_url ' => ' http://openapi.qzone.qq.com/user/get_user_info ',
);
$action = Isset ($_get[' action ')? $_get[' action ']: ';
$QQ = new Qq_oauth ($qq _oauth_config);
Switch ($action) {
User Login Step1: Request Temporary token
Case ' login ':
$token = $qq->oauth_request_token ();
$_session[' oauth_token_secret ' = $token [' Oauth_token_secret '];
$QQ->authorize ($token [' Oauth_token ']);
Break
Step4:qzone directs users to third party applications
Case ' Reg ':
$qq->register_user ();
$access _token = $qq->request_access_token ();
if ($token = $qq->save_access_token ($access _token)) {
Save, generally sent to user cookies, and user warehousing
Var_dump ($token);
$_session[' oauth_token ' = $token [' Oauth_token '];
$_session[' oauth_token_secret ' = $token [' Oauth_token_secret '];
$_session[' OpenID '] = $token [' OpenID '];
Header (' content-type:text/html; Charset=utf-8 ');
$user _info = Json_decode ($qq->get_user_info ());
if ($user _info->ret!=0) {
Exit ("An error occurred while getting the avatar nickname". $user _info->msg);
} else {
Echo ' QQ nickname: ', $user _info->nickname,
' figureurl, '/> ',
' figureurl_1, '/> ',
' figureurl_2, '/> ';
}
}
Break
Default:
}
Class qq_oauth{
Private $config;
function __construct ($config) {
$this->config = $config;
}
/**
* Return configuration
* @param string $name
*
*/
function C ($name) {
return Isset ($this->config[$name])? $this->config[$name]: FALSE;
}
/**
* Build Request URL
* @param string $url
* @param array $params
* @param string $oauth _token_secret
*
*/
function Build_request_uri ($url, $params =array (), $oauth _token_secret= ') {
$oauth _consumer_key = $this->c (' Oauth_consumer_key ');
$oauth _consumer_secret = $this->c (' Oauth_consumer_secret ');
$params = Array_merge (Array (
' Oauth_version ' => ' 1.0 ',
' Oauth_signature_method ' => ' hmac-sha1 ',
' Oauth_timestamp ' =>time (),
' Oauth_nonce ' =>rand (1000,99999999),
' Oauth_consumer_key ' => $oauth _consumer_key,
), $params);
$encode _params = $params;
Ksort ($encode _params);
$oauth _signature = ' get& '. UrlEncode ($url). ' & ' UrlEncode (Http_build_query ($encode _params));
$oauth _signature = Base64_encode (Hash_hmac (' SHA1 ', $oauth _signature, $oauth _consumer_secret.) & $oauth _token_secret,true));
$params [' oauth_signature '] = $oauth _signature;
return $url. '? '. Http_build_query ($params);
}
/**
* Verify that the callback returns the parameters of the contract
*/
function Check_callback () {
if (Isset ($_get[' Oauth_token '))
if (Isset ($_get[' OpenID '))
if (Isset ($_get[' oauth_signature '))
if (isset ($_get[' timestamp '))
if (Isset ($_get[' Oauth_vericode '))
return true;
return false;
}
function Get_contents ($url) {
$curl = Curl_init ();
curl_setopt ($curl, curlopt_returntransfer,true);
curl_setopt ($curl, Curlopt_url, $url);
return curl_exec ($curl);
}
/**
* STEP1: Request temporary token, STEP2: Generate unauthorized Temporary token
*/
function Oauth_request_token () {
$url = $this->build_request_uri ($this->c (' Oauth_request_token_url '));
$tmp _oauth_token = $this->get_contents ($url);
Parse_str ($tmp _oauth_token);
/*
Oauth_token Unauthorized Temporary token
Oauth_token_secret token key, which is limited to temporary token
Error_code error code
*/
if (Isset ($error _code)) exit ($error _code);
Return Array (
' Oauth_token ' => $oauth _token,
' Oauth_token_secret ' => $oauth _token_secret
);
}
/**
* STEP3: Guide users to the Qzone login page
* @param string $oauth _token an unauthorized temporary token
*/
function authorize ($oauth _token) {
$STR = "http/1.1 302 Found";
Header ($STR);
$url = $this->c (' Oauth_authorize_url ');
$query _strings = http_build_query (Array (
' Oauth_consumer_key ' => $this->c (' Oauth_consumer_key '),
' Oauth_token ' => $oauth _token,
' Oauth_callback ' => $this->c (' Oauth_callback '),
));
Header (' Location: '. $url. '? '. $query _strings);
}
/**
* Step4:qzone to redirect users to third party applications
* @return BOOL Validation is valid
*/
function Register_user () {
/*
* Oauth_token Authorized Temporary Token
* OpenID Tencent User external Unified ID, the OpenID and the user QQ number one by one corresponding
* Oauth_signature signature value to facilitate third parties to verify OpenID and source reliability.
* Using the HMAC-SHA1 algorithm:
* Source string: Openid+timestamp (do not add ' + ' symbol in the middle of string)
* Key: Oauth_consumer_secret
* Timestamp OpenID time stamp
* Oauth_vericode Authorization Verification Code.
*/
if ($this->check_callback ()) {
Verifying signatures
$signature = Base64_encode (Hash_hmac (' SHA1 ', $_get[' OpenID '].$_get[' timestamp '), $this->c (' Oauth_consumer_ Secret '), true);
if (!emptyempty ($_get[' oauth_signature ')) && $signature ==$_get[' oauth_signature ']) {
$_session[' oauth_token '] = $_get[' Oauth_token '];
$_session[' oauth_vericode '] = $_get[' Oauth_vericode '];
Return
}
}
Checksum not passed
Exit (' Unknow REQUEST ');
}
/**
* STEP5: Request access token
*/
function Request_access_token () {
$url = $this->build_request_uri ($this->c (' Oauth_request_access_token_url '), Array (
' Oauth_token ' =>$_session[' Oauth_token '],
' Oauth_vericode ' =>$_session[' Oauth_vericode ']
), $_session[' Oauth_token_secret ']);
return $this->get_contents ($url);
}
/**
* STEP6: Generate access token (Save access token)
*
* About Access_token
* The current Access_token (and its secret) is long-term effective, and an OpenID corresponding, currently can support offline access to the information of the OpenID.
* Of course, the user has the right to remove the authorization of the third party on the Qzone side, at this time the Access_token will be invalidated, need to go back to the entire process to allow users to authorize.
* Later will gradually enrich the effectiveness of access_token, long-term effective, short-term effective, user login only effective.
*/
function Save_access_token ($access _token_str) {
Parse_str ($access _token_str, $access _token_arr);
if (isset ($access _token_arr[' Error_code ')) {
return FALSE;
} else {
return $access _token_arr;
}
}
/**
* Tencent currently only open the API
* Obtain the login user information, currently can obtain the user nickname and Avatar information.
* Http://openapi.qzone.qq.com/user/get_user_info
*/
function Get_user_info () {
$url = $this->build_request_uri ($this->c (' User_info_url '), Array (
' Oauth_token ' =>$_session[' Oauth_token '],
' OpenID ' =>$_session[' OpenID '],
), $_session[' Oauth_token_secret ']);
return $this->get_contents ($url);
}
}

File package Download Qq_php.rar
Turn from: http://dev.meettea.com

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.