qq 登录api php类(3)
来源:未知 责任编辑:责任编辑 发表时间:2015-09-16 20:04 点击:次
//必要参数,不要随便更改!!
$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;
//对参数按照字母升序做序列化
$normalized_str = $this->get_normalized_string($params);
$sigstr .= rawurlencode($normalized_str);
//echo “sigstr = $sigstr”;
//(2)构造密钥
$key = $this->appkey.”&”.$request_token_secret;
//(3)生成oauth_signature签名值。这里需要确保PHP版本支持hash_hmac函数
$signature = $this->get_signature($sigstr, $key);
//构造请求url
$url .= $normalized_str.”&”.”oauth_signature=”.rawurlencode($signature);
return file_get_contents($url);
}
/**
* @brief 对参数进行字典升序排序
*
* @param $params 参数列表
*
* @return 排序后用&链接的key-value对(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 使用HMAC-SHA1算法生成oauth_signature签名值
*
* @param $key 密钥
* @param $str 源串
*
* @return 签名值
*/
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(0×00));
$ipad = str_repeat(chr(0×36),$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编码,遵循rfc1738 urlencode
*
* @param $params
*
* @return URL编码后的字符串
*/
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>