PHP QQ登录接口应用
//---------------------------
<?php
class qzone
{
var $appid = "***********";//换成您的
var $appkey = "**********";//换成您的
//除去数组中的空值和签名模式
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;
}
/**
* 生成请求代码
* @param String $callback 回调地址
*/
function request($callback = )
{
//必要参数,不要随便更改!!
$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]))
{
//错误返回输出
die();
}
$_SESSION["qq_token"] = $result["oauth_token"];
$_SESSION["qq_secret"] = $result["oauth_token_secret"];
//302跳转到授权页面
$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");
}
/**
* 响应操作
*/
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;
}
//验证签名
$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;
}
//链接字符串
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)
相关新闻>>
- 发表评论
-
- 最新评论 更多>>