利用 PHP 和 Follow5 同步腾讯微博
如果你是个微博同步控,这篇文章可以助你一臂之力。首先 Follow5 可以用来同步几乎所有微博,不过大名鼎鼎的腾讯微博不可以,那么本文就此应运而生。编程语言用的PHP,运行在开启了 curl 的 PHP 服务器上,所以首先要确定你的服务器开启了 curl。本段代码主要是实现发微博到 Follow5 和腾讯微博,利用 Follow5 的五步大同功能,就不必再发其它微博上了。
本文主要参考了三篇文章:腾讯微博API,同步消息到腾讯微博,发布一个Follow5的API PHP类 。因此如果你对本文有任何不理解的地方,可以详细阅读以上文章。
闲话少说,发表微博到 Follow5,你还得需要一个 Follow5 的 key,在这里我要表扬 Follow5 办事效率高,一个晚上就将 key 发给我了,太高兴了。以下就是代码,如果因为代码损害了原博主,还请告知。本文对 PHP 和 python 一窍不通,所以几乎都是啃大家的代码然后加以组合。
<?php
if ($_POST['status']) {
$status = $_POST['status'];
update_follow5('username', 'password', $status);
//此处的username是你的腾讯微博用户名,而不是你的Q号码
update_qq('username', 'password', $status);
//跳回到发表微博的界面
header("location: xxxxx.html");
}
function update_follow5($username, $password, $status){
//这个api_key需要自己去向Follow5申请
$api_key = 'xxxxxxxxxxxxxxxx';
$usr = $username.':'.$password;
$str = 'status='.urlencode(stripslashes($status)).'&api_key='.$api_key;
$url = 'http://api.follow5.com/api/statuses/update.xml';
postCurl($url, $usr, $str);
}
//此处代码参考http://www.js8.in/349.html
function postCurl($url, $usr, $str) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $usr);
curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
$data = curl_exec($ch);
$Headers = curl_getinfo($ch);
if ($Headers['http_code'] == 200) {
return $data;
} else {
if ($Headers['http_code'] == 401) {
echo 'Username and or password not set,or api key is wrong';
} elseif ($Headers['http_code'] == 404) {
echo 'Invalud URL for CURL request';
}
}
}
function update_qq($qq, $password, $content){
//省却代码无数,补充代码请参看http://www.ineeke.com/archives/tencent-microblog-api/
//增加这第五次是用来发表腾讯微博的
//改装来自于http://isouth.org/archives/279.html
$fields = 'content='.$content.'&countType=&pic=&viewModel=0';
$referer_url='http://t.qq.com/oooooooooooooooooooooooo';
$url='http://t.qq.com/publish.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_REFERER, $referer_url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar);
$loginResult = curl_exec($curl);
curl_close($curl);
unlink($cookie_jar);
}
?>
据说腾讯的登录有时会出问题的,所以如果在使用过程出现问题,还请能安心对之,不过我现在还没出过什么问题,可能测试次数少的缘故吧。更多知识还请去上面提到的三位博主的博客吧,他们才是真正的牛人。
本文转载自转烛空间,原文地址:http://wangliping.net/sync-weibo。





9 条留言
我要留言起衣 | 2010/09/05 10:30 上午
说起来,我也能算是同步控叻
[回复]
影浅 | 2010/09/05 10:30 上午
我习惯用qq签名了,但是不错,
支持下
[回复]
小康 | 2010/09/05 10:30 上午
不错,支持个
[回复]
爱月 | 2010/09/05 10:30 上午
除了腾讯的联络功能外其余功能不使用的飘。。。
[回复]
火炬之光 | 2010/09/05 10:30 上午
不错,follow5+你这个就完美了!
[回复]
Neeke | 2010/09/05 10:30 上午
呵呵 都开始自由发挥了。
[回复]
@kelven | 2010/09/05 10:30 上午
有办法同步到腾讯微薄图片吗?
[回复]
XDash 回复:
十月 15th, 2010 at 14:34
@@kelven, 目前没有出现。。
[回复]
凡尘飞扬 | 2010/09/05 10:30 上午
没有follw5的微博,不过还是参考一下你的代码
[回复]