PHP QQ登录接口应用
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);
}
}
//---------------------------
?>
使用方法
请求代码
//---------------------------
//请求
$qzone = new qzone;
$qzone->request(域名/qzone/login);
//---------------------------
?>
接受代码
//---------------------------
//接收
if ($qzone->respond())
{
//如果成功,获取用户信息
$result = $qq->getHttpRequest("http://openapi.qzone.qq.com/user/get_user_info", $_SESSION["qq_token"], $_SESSION["qq_secret"], $_SESSION["qq_openid"]);
//下面是数据库操作业务
}
//---------------------------
?>
下一篇: PHP面向对象编程——特殊实践DAY 4