//---------------------------
<?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)
{
$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;
}
/**
* 遠程獲取數據
* $url 指定URL完整路徑地址
* @param $input_charset 編碼格式。默認值:空值
* @param $time_out 超時時間。默認值:60
* return 遠程輸出的數據
*/
function getHttpRequest($url, $access_token, $access_token_secret, $openid)
{
$sigstr = "GET"."&".rawurlencode("$url")."&";
//必要參數, 不要隨便更改!!
$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);
}
}
//---------------------------
?>
使用方法
請求代碼
<?php
//---------------------------
//請求
$qzone = new qzone;
$qzone->request(域名/qzone/login);
//---------------------------
?>
接受代碼
<?php
//---------------------------
//接收
if ($qzone->respond())
{
//如果成功,獲取用戶信息
$result = $qq->getHttpRequest("http://openapi.qzone.qq.com/user/get_user_info", $_SESSION["qq_token"], $_SESSION["qq_secret"], $_SESSION["qq_openid"]);
//下面是數據庫操作業務
}
//---------------------------
?>