QQ Login API PHP Class _php tutorial

Source: Internet
Author: User
Tags hmac oauth openid php website rfc sha1
/**
* QQ Operation class
* $Author: Shunzi $
* $Id: cls_qq.php 17171 2011-05-30 06:14:00z Shunzi $
*/
Class Qq_api
{
var $appid = ";
var $appkey = ";
var $callback = ";
var $login _type = 1;
/**
* Constructor function
*
* @access Public
* @param string $ver version number
*
* @return void
*/
function Qq_api ($appid, $appkey, $callback, $login _type)
{
$this->appid = $appid;
$this->appkey = $appkey;
$this->callback = $callback;
$this->login_type = $login _type;
}
/**
* @brief jump to the QQ login page. The request must be URL encoded, please follow RFC 1738 when encoding
*
* @param $appid
* @param $appkey
* @param $callback
*
* @return Return string format is: oauth_token=xxx&openid=xxx&oauth_signature=xxx&timestamp=xxx&oauth_vericode= Xxx
*/
function Redirect_to_login ()
{
Jump to the interface address of the QQ login page, do not change!!
$redirect = "http://openapi.qzone.qq.com/oauth/qzoneoauth_authorize?oauth_consumer_key=". $this->appid. " & ";
Call the Get_request_token interface for unauthorized temporary tokens
$result = Array ();
$request _token = $this->get_request_token ();
Parse_str ($request _token, $result);
Request token, request token secret need to be saved.
In the demo demo, it is saved directly in the global variable.
To prevent a site from having multiple subdomains or the same primary domain, the session cannot be shared by a different server
Ask the developer to make the necessary changes to the session.php in accordance with the comments in comm/session.php in this SDK to address the 2 above questions,
$_session["token"] = $result ["Oauth_token"];
$_session["secret"] = $result ["Oauth_token_secret"];
Print_r ($_session);
if ($result ["oauth_token"] = = "")
{
The error condition is not handled in the sample code. Real-world Web sites need to handle error situations on their own
Exit
}
Construct the request URL
$redirect. = "oauth_token=". $result ["Oauth_token"]. " &oauth_callback= ". Rawurlencode ($this->callback);
Header ("Location: $redirect");
}
/**
* @brief Request a temporary token. The request must be URL encoded, please follow RFC 1738 when encoding
*
* @param $appid
* @param $appkey
*
* @return Return string format is: oauth_token=xxx&oauth_token_secret=xxx
*/
function Get_request_token ()
{
Request the interface address of the temporary token, do not change!!
$url = "Http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token?";
Generates a Oauth_signature signature value. The method of generating signature values is described in http://wiki.opensns.qq.com/wiki/"QQ login" signature parameter oauth_signature.
(1) Constructs the source string that generates the signature value (HTTP request method & UrlEncode (URI) & UrlEncode (A=x&b=y& ...) )
$sigstr = "GET". " & ". Rawurlencode (" Http://openapi.qzone.qq.com/oauth/qzoneoauth_request_token ")." & ";
Necessary parameters
$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;
Serialization of parameters in ascending alphabetical order
$normalized _str = $this->get_normalized_string ($params);
$sigstr. = Rawurlencode ($normalized _str);
(2) Construction key
$key = $this->appkey. " & ";
(3) Generate Oauth_signature signature value. Here we need to make sure PHP version supports Hash_hmac function
$signature = $this->get_signature ($sigstr, $key);
Construct the request URL
$url. = $normalized _str. " & "." Oauth_signature= ". Rawurlencode ($signature);
echo "$SIGSTR \ n";
echo "$url \ n";
Return file_get_contents ($url);
}
/*
* @brief get user information. The request is URL encoded, please follow RFC 1738 when encoding
*
* @param $appid
* @param $appkey
* @param $access _token
* @param $access _token_secret
* @param $openid
*
*/
function Get_user_info ($access _token, $access _token_secret, $openid)
{
Get the interface address of user information, do not change!!
$url = "Http://openapi.qzone.qq.com/user/get_user_info";
$info = $this->do_get ($url, $access _token, $access _token_secret, $openid);
$arr = Array ();
$arr = Json_decode ($info, true);
return $arr;
}
/**
* @brief get Access_token. The request is URL-encoded and is encoded in accordance with RFC 1738
*
* @param $appid
* @param $appkey
* @param $request _token
* @param $request _token_secret
* @param $vericode
*
* @return Return string format is: Oauth_token=xxx&oauth_token_secret=xxx&openid=xxx&oauth_signature=xxx&oauth_ Vericode=xxx&timestamp=xxx
*/
function Get_access_token ($request _token, $request _token_secret, $vericode)
{
Request the interface address of the Access_token with Qzone access, do not change!!
$url = "Http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token?";
Generates a Oauth_signature signature value. The method of generating signature values is described in http://wiki.opensns.qq.com/wiki/"QQ login" signature parameter oauth_signature.
(1) Constructs the source string that generates the signature value (HTTP request method & UrlEncode (URI) & UrlEncode (A=x&b=y& ...) )
$sigstr = "GET". " & ". Rawurlencode (" Http://openapi.qzone.qq.com/oauth/qzoneoauth_access_token ")." & ";
necessary parameters, do not change casually!!
$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"] = $request _token;
$params ["oauth_vericode"] = $vericode;
Serialization of parameters in ascending alphabetical order
$normalized _str = $this->get_normalized_string ($params);
$sigstr. = Rawurlencode ($normalized _str);
echo "sigstr = $sigstr";
(2) Construction key
$key = $this->appkey. " & ". $request _token_secret;
(3) Generate Oauth_signature signature value. Here we need to make sure PHP version supports Hash_hmac function
$signature = $this->get_signature ($sigstr, $key);
Construct the request URL
$url. = $normalized _str. " & "." Oauth_signature= ". Rawurlencode ($signature);
Return file_get_contents ($url);
}
/**
* @brief dictionary ascending order of parameters
*
* @param $params parameter list
*
* @return after sorting with & link key-value pair (key1=value1&key2=value2 ...)
*/
function get_normalized_string ($params)
{
Ksort ($params);
$normalized = Array ();
foreach ($params as $key = $val)
{
$normalized [] = $key. " = ". $val;
}
Return implode ("&", $normalized);
}
/**
* @brief generate Oauth_signature signature values using the HMAC-SHA1 algorithm
*
* @param $key Key
* @param $str Source string
*
* @return Signature Value
*/
function Get_signature ($STR, $key)
{
$signature = "";
if (function_exists (' Hash_hmac '))
{
$signature = Base64_encode (Hash_hmac ("Sha1″, $str, $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). $str
)
)
)
);
$signature = Base64_encode ($HMAC);
}
return $signature;
}
/**
* @brief URL encoding of strings, follow rfc1738 UrlEncode
*
* @param $params
*
* @return URL-encoded string
*/
function get_urlencode_string ($params)
{
Ksort ($params);
$normalized = Array ();
foreach ($params as $key = $val)
{
$normalized [] = $key. " = ". Rawurlencode ($val);
}
Return implode ("&", $normalized);
}
/**
* @brief Check if OpenID is legal
*
* @param $openid with the user QQ number one by one corresponding
* @param $timestamp time stamp
* @param $sig Signature Value
*
* @return True or False
*/
function Is_valid_openid ($openid, $timestamp, $sig)
{
$key = $this->appkey;
$str = $openid. $timestamp;
$signature = $this->get_signature ($str, $key);
echo "SIG: $sig \ n";
echo "str: $STR \ n";
return $sig = = $signature;
}
/**
* This method can be used @brief all get requests
*
* @param $url
* @param $appid
* @param $appkey
* @param $access _token
* @param $access _token_secret
* @param $openid
*
* @return True or False
*/
function Do_get ($url, $access _token, $access _token_secret, $openid)
{
$sigstr = "GET". " & ". Rawurlencode (" $url ")." & ";
necessary parameters, do not change casually!!
$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"]);
Parameters are serialized in ascending alphabetical order
$normalized _str = $this->get_normalized_string ($params);
$sigstr. = Rawurlencode ($normalized _str);
Signature, ensure PHP version supports HASH_HMAC function
$key = $this->appkey. " & ". $access _token_secret;
$signature = $this->get_signature ($sigstr, $key);
$url. = "?". $normalized _str. " & "." Oauth_signature= ". Rawurlencode ($signature);
echo "$url \ n";
Return file_get_contents ($url);
}
/**
* This method can be used @brief all multi-part post requests
*
* @param $url
* @param $appid
* @param $appkey
* @param $access _token
* @param $access _token_secret
* @param $openid
*
*/
function Do_multi_post ($url, $appid, $appkey, $access _token, $access _token_secret, $openid)
{
Constructs a signature string. Source string: Method [get| post]&uri& parameters in ascending alphabetical order
$sigstr = "POST". " & "." $url "." & ";
necessary parameters, do not change casually!!
$params = $_post;
$params ["oauth_version"] = "1.0″;
$params ["oauth_signature_method"] = "hmac-sha1″;
$params ["oauth_timestamp"] = time ();
$params ["oauth_nonce"] = Mt_rand ();
$params ["oauth_consumer_key"] = $appid;
$params ["oauth_token"] = $access _token;
$params ["OpenID"] = $openid;
unset ($params ["oauth_signature"]);
Get Upload picture information
foreach ($_files as $filename = $filevalue)
{
if ($filevalue ["error"]! = UPLOAD_ERR_OK)
{
echo "Upload file error $filevalue [' Error ']\n ';
Exit
}
$params [$filename] = file_get_contents ($filevalue ["Tmp_name"]);
}
Serialization of parameters in ascending alphabetical order
$sigstr. = $this->get_normalized_string ($params);
Signature, need to ensure PHP version supports HASH_HMAC function
$key = $appkey. " & ". $access _token_secret;
$signature = $this->get_signature ($sigstr, $key);
$params ["oauth_signature"] = $signature;
Working with uploaded images
foreach ($_files as $filename = $filevalue)
{
$tmpfile = DirName ($filevalue ["Tmp_name"]). " /". $filevalue [" name "];
Move_uploaded_file ($filevalue ["Tmp_name"], $tmpfile);
$params [$filename] = "@ $tmpfile";
}
/*
echo "Len:". strlen ($SIGSTR). " \ n ";
echo "SIG: $sigstr \ n";
echo "Key: $appkey &\n";
*/
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_returntransfer, TRUE);
curl_setopt ($ch, Curlopt_post, TRUE);
curl_setopt ($ch, Curlopt_postfields, $params);
curl_setopt ($ch, Curlopt_url, $url);
$ret = curl_exec ($ch);
$httpinfo = Curl_getinfo ($ch);
Print_r ($httpinfo);
Curl_close ($ch);
Delete upload Temporary files
Unlink ($tmpfile);
return $ret;
}
/**
* This method can be used @brief all post requests
*
* @param $url
* @param $appid
* @param $appkey
* @param $access _token
* @param $access _token_secret
* @param $openid
*
*/
function Do_post ($url, $appid, $appkey, $access _token, $access _token_secret, $openid)
{
Constructs a signature string. Source string: Method [get| post]&uri& parameters in ascending alphabetical order
$sigstr = "POST". " & ". Rawurlencode ($url)." & ";
necessary parameters, do not change casually!!
$params = $_post;
$params ["oauth_version"] = "1.0″;
$params ["oauth_signature_method"] = "hmac-sha1″;
$params ["oauth_timestamp"] = time ();
$params ["oauth_nonce"] = Mt_rand ();
$params ["oauth_consumer_key"] = $appid;
$params ["oauth_token"] = $access _token;
$params ["OpenID"] = $openid;
unset ($params ["oauth_signature"]);
Serialization of parameters in ascending alphabetical order
$sigstr. = Rawurlencode ($this->get_normalized_string ($params));
Signature, need to ensure PHP version supports HASH_HMAC function
$key = $appkey. " & ". $access _token_secret;
$signature = $this->get_signature ($sigstr, $key);
$params ["oauth_signature"] = $signature;
$postdata = $this->get_urlencode_string ($params);
echo "$sigstr ******\n";
echo "$postdata \ n";
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_returntransfer, TRUE);
curl_setopt ($ch, Curlopt_post, TRUE);
curl_setopt ($ch, Curlopt_postfields, $postdata);
curl_setopt ($ch, Curlopt_url, $url);
$ret = curl_exec ($ch);
Curl_close ($ch);
return $ret;
}
}
?>
Temporarily only login feature, the method in post-project continues to update

From the construction of CIS network PHP website

http://www.bkjia.com/PHPjc/478352.html www.bkjia.com true http://www.bkjia.com/PHPjc/478352.html techarticle PHP/** * QQ Operation class * $Author: Shunzi $ * $Id: cls_qq.php 17171 2011-05-30 06:14:00z Shunzi $ */class Qq_api {var $appi D =; var $appkey =; var $callback =; var $login _type = ...

  • 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.