Php qq login interface application

Source: Internet
Author: User

//---------------------------
<? Php
Class qzone
{
Var $ appid = "************"; // replace it with your
Var $ appkey = "***********"; // replace it with your

// Remove null values and signature modes from the array
Function filter ($ parameter)
{
$ Para = array ();
While (list ($ key, $ val) = each ($ parameter ))
{
If ($ key = "sign" | $ key = "sign_type" | $ val = "")
{
Continue;
}
Else
{
$ Para [$ key] = $ parameter [$ key];
}
}
Return $ para;
}

/**
* Generate Request Code
* @ Param String $ callback address
*/
Function request ($ callback =)
{
// Required parameters. Do not change them !!
$ Params = array ();
$ Params ["oauth_version"] = "1.0 ";
$ Params ["oauth_signature_method"] = "HMAC-SHA1 ";
$ Params ["oauth_timestamp"] = time ();
$ Params ["oauth_nonce"] = mt_rand ();
$ Params ["oauth_consumer_key"] = $ this-> appid;

$ Sign = $ this-> sign ($ params, "GET". "&". rawurlencode ("http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token"). "&", $ this-> appkey .&);

$ Url = "format ($ params)." & "." oauth_signature = ". rawurlencode ($ sign> http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token? ". $ This-> format ($ params)." & "." oauth_signature = ". rawurlencode ($ sign );

$ Request = file_get_contents ($ url); // print_r ($ url); print_r ($ request); die;

$ Result = array ();

Parse_str ($ request, $ result );

If (! Isset ($ result [oauth_token])
{
// Error returned output
Die ();
}

$ _ SESSION ["qq_token"] = $ result ["oauth_token"];
$ _ SESSION ["qq_secret"] = $ result ["oauth_token_secret"];

// 302 jump to the authorization page
$ Redirect. = "appid. "& oauth_token = ". $ result ["oauth_token"]. "& oauth_callback = ". rawurlencode ($ callback); // die ($ redirect> http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize? Oauth_consumer_key = ". $ this-> appid. "& oauth_token = ". $ result ["oauth_token"]. "& oauth_callback = ". rawurlencode ($ callback); // die ($ redirect );
Header ("Location: $ redirect ");
}

/**
* Response operations
*/
Function respond ()
{
$ Params = array ();
$ Params ["oauth_version"] = "1.0 ";
$ Params ["oauth_signature_method"] = "HMAC-SHA1 ";
$ Params ["oauth_timestamp"] = time ();
$ Params ["oauth_nonce"] = mt_rand ();
$ Params ["oauth_consumer_key"] = $ this-> appid;
$ Params ["oauth_token"] = $ _ SESSION ["qq_token"];
$ Params ["oauth_vericode"] = $ _ REQUEST ["oauth_vericode"];

// Echo "sig: $ sig/n ";
// Echo "str: $ str/n ";

$ Sign = $ this-> sign ($ params, "GET ". "&". rawurlencode ("http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token "). "&", $ this-> appkey. &. $ _ SESSION ["qq_secret"]);

$ Url = "format ($ params)." & "." oauth_signature = ". rawurlencode ($ sign> http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token? ". $ This-> format ($ params)." & "." oauth_signature = ". rawurlencode ($ sign );

$ Request = file_get_contents ($ url );

$ Result = array ();

Parse_str ($ request, $ result );

If (isset ($ result ["error_code"])
{
Return false;
}

// Verify the signature
$ Key = $ this-> appkey;
$ Str = $ _ REQUEST ["openid"]. trim ($ _ REQUEST ["timestamp"]);

If ($ _ REQUEST ["oauth_signature"]! = $ This-> sign (array (), $ str, $ key ))
{
Return false;
}

$ _ SESSION ["qq_token"] = $ result ["oauth_token"];
$ _ SESSION ["qq_secret"] = $ result ["oauth_token_secret"];
$ _ SESSION ["qq_openid"] = $ result ["openid"];


Return true;
}


// Link string
Function format ($ parameter)
{
$ Parameter = $ this-> filter ($ parameter );

Ksort ($ parameter );
Reset ($ parameter );

$ Params =;
Foreach ($ parameter AS $ key => $ val)
{
$ Params. = "$ key = $ val &";
}

$ Params = substr ($ params, 0,-1 );
Return $ params;
}

Function sign ($ parameter, $ source, $ key)
{
$ Params = $ source. rawurlencode ($ this-> format ($ parameter ));
// $ Key = $ this-> appkey. &. $ secret;
$ Sign = "";
If (function_exists (hash_hmac ))
{
$ Sign = base64_encode (hash_hmac ("sha1", $ params, $ key, true ));
}
Else
{
$ Blocksize = 64;
$ Hashfunc = sha1;
If (strlen ($ key)> $ blocksize)
{
$ Key = pack (H *, $ hashfunc ($ key ));
}
$ Key = str_pad ($ key, $ blocksize, chr (0x00 ));
$ Ipad = str_repeat (chr (0x36), $ blocksize );
$ Opad = str_repeat (chr (0x5c), $ blocksize );
$ Hmac = pack (
H *, $ hashfunc (
($ Key ^ $ opad). pack (
H *, $ hashfunc (
($ Key ^ $ ipad). $ params
)
)
)
);
$ Sign = base64_encode ($ hmac );
}

Return $ sign;
}

/**
* Remote Data Retrieval
* $ Url specifies the complete URL address
* @ Param $ input_charset encoding format. Default Value: NULL
* @ Param $ time_out timeout. Default Value: 60
* Return remote output data
*/
Function getHttpRequest ($ url, $ access_token, $ access_token_secret, $ openid)
{
$ Sigstr = "GET". "&". rawurlencode ("$ url ")."&";

// Required parameters. Do not change them !!
$ Params = $ _ GET;
$ Params ["oauth_version"] = "1.0 ";
$ Params ["oauth_signature_method"] = "HMAC-SHA1 ";
$ Params ["oauth_timestamp"] = time ();
$ Params ["oauth_nonce"] = mt_rand ();
$ Params ["oauth_consumer_key"] = $ this-> appid;
$ Params ["oauth_token"] = $ access_token;
$ Params ["openid"] = $ openid;
Unset ($ params ["oauth_signature"]);

$ Sign = $ this-> sign ($ params, "GET ". "&". rawurlencode ("$ url "). "&", $ this-> appkey. &. $ _ SESSION ["qq_secret"]);

$ Url = $ url ."? ". $ This-> format ($ params)." & "." oauth_signature = ". rawurlencode ($ sign );

$ Result = file_get_contents ($ url );

Return json_decode (str_replace (array ("/n", "/t"), $ result), true );
}
}

//---------------------------
?>

Usage
Request Code
<? Php
//---------------------------
// Request
$ Qzone = new qzone;
$ Qzone-> request (Domain Name/qzone/login );
//---------------------------
?>
Accept code
<? Php
//---------------------------
// Receive
If ($ qzone-> respond ())
{
// If successful, obtain user information
$ Result = $ qq-> getHttpRequest ("http://openapi.qzone.qq.com/user/get_user_info", $ _ SESSION ["qq_token"], $ _ SESSION ["qq_secret"], $ _ SESSION ["qq_openid"]);

// Below is the database operation service
}
//---------------------------
?>

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.