增加“我的微博”
微博大道而行,我也得跟上:
1、顶部栏目新增“我的微博”,自动采集最新20条,来源有我的新浪微博和腾讯微博,抓取的缓冲时间为300秒;腾讯微博抓取偶尔有“check sign error”错误,略不稳定,不知是腾讯接口问题,还是偶对oauth_signature的算法实现有问题,待观察
2、左侧栏目增加微博入口
鉴于微博口水较多,暂不加入RSS输出,酱紫~~~
写这个页面的时候,粗略看了“腾讯微博开放平台”和“新浪微博开放平台”API文档中OAuth和timeline的一些描述、范例和接口说明,前者的文档需要努力呀,大小写错误、描述不清晰等问题较多。
---------------以下 2011年5月5日补充------------------
“腾讯微博抓取偶尔有“check sign error”错误”,这个问题找到了,是由于生成的oauth_signature拼装到url的时候,没有做urlencode,当其中包含+字符的时候,参数传递就会出现问题;其他参数也忘记了,一并加上。
附:以调用腾讯“broadcast_timeline 我发表时间线”为例的PHP片段代码,主要为了说明oauth_signature的生成和url的拼装,其中考虑有同名参数时需再按value排序(If two or more parameters share the same name, they are sorted by their value):
[quote]$QQ_APP_KEY = "xxx";
$QQ_APP_SECRET = "xxx";
$QQ_AUTH_TOKEN = "xxx"; //Request Token
$QQ_TOKEN_SECRET = "xxx"; //Request Token Secret
$QQ_BROADCAST_TIMELINE_URL = "http://open.t.qq.com/api/statuses/broadcast_timeline";
$QQ_HTTP_METHOD = "GET";
$QQ_DEFAULT_COUNT_NUMBER = 20;
$QQ_BROADCAST_TIMELINE_PARAMS = array (
"format" => "json",
"oauth_consumer_key" => $QQ_APP_KEY,
"oauth_signature_method" => "HMAC-SHA1",
"oauth_token" => $QQ_AUTH_TOKEN,
"oauth_version" => "1.0",
"pageflag" => 0,
"pagetime" => 0,
"reqnum" => $QQ_DEFAULT_COUNT_NUMBER,
"oauth_timestamp" => "",
"oauth_nonce" => ""
);
//init variable params
$QQ_BROADCAST_TIMELINE_PARAMS["oauth_timestamp"] = time();
$QQ_BROADCAST_TIMELINE_PARAMS["oauth_nonce"] = md5(uniqid(rand()));
//sort params: If two or more parameters share the same name, they are sorted by their value
ksort($QQ_BROADCAST_TIMELINE_PARAMS);
foreach ($QQ_BROADCAST_TIMELINE_PARAMS as $k => $v)
{
if (is_array($v))
{
sort($v);
$QQ_BROADCAST_TIMELINE_PARAMS[$k] = $v;
}
}
$params = "";
foreach ($QQ_BROADCAST_TIMELINE_PARAMS as $k => $v)
{
if (is_array($v))
{
foreach ($v as $k2 => $v2)
{
$params .= urlencode($k)."%3D".urlencode($v2)."%26";
}
}
else
{
$params .= urlencode($k)."%3D".urlencode($v)."%26";
}
}
$params = substr($params, 0, strlen($params) - 3);
//generate signature base string, key and oauth signature
$signature_base = $QQ_HTTP_METHOD."&".urlencode(strtolower($QQ_BROADCAST_TIMELINE_URL))."&".$params;
$signature_key = $QQ_APP_SECRET."&".$QQ_TOKEN_SECRET;
$oauth_signature = base64_encode(hash_hmac("sha1", $signature_base, $signature_key, true));
//generate full request url
$url = $QQ_BROADCAST_TIMELINE_URL."?";
foreach ($QQ_BROADCAST_TIMELINE_PARAMS as $k => $v)
{
if (is_array($v))
{
foreach ($v as $k2 => $v2)
{
$url .= urlencode($k)."=".urlencode($v2)."&";
}
}
else
{
$url .= urlencode($k)."=".urlencode($v)."&";
}
}
$url .= "oauth_signature=".urlencode($oauth_signature);
//get remote data by url
...
[/quote]
发表评论