如果你是个微博同步控,这篇文章可以助你一臂之力。首先 Follow5 可以用来同步几乎所有微博,不过大名鼎鼎的腾讯微博不可以,那么本文就此应运而生。编程语言用的PHP,运行在开启了 curl 的 PHP 服务器上,所以首先要确定你的服务器开启了 curl。本段代码主要是实现发微博到 Follow5 和腾讯微博,利用 Follow5 的五步大同功能,就不必再发其它微博上了。
本文主要参考了三篇文章:腾讯微博API,同步消息到腾讯微博,发布一个Follow5的API PHP类 。因此如果你对本文有任何不理解的地方,可以详细阅读以上文章。
闲话少说,发表微博到 Follow5,你还得需要一个 Follow5 的 key,在这里我要表扬 Follow5 办事效率高,一个晚上就将 key 发给我了,太高兴了。以下就是代码,如果因为代码损害了原博主,还请告知。本文对 PHP 和 python 一窍不通,所以几乎都是啃大家的代码然后加以组合。
02 |
if ( $_POST [ 'status' ]) { |
03 |
$status = $_POST [ 'status' ]; |
04 |
update_follow5( 'username' , 'password' , $status ); |
07 |
update_qq( 'username' , 'password' , $status ); |
10 |
header( "location: xxxxx.html" ); |
13 |
function update_follow5( $username , $password , $status ){ |
15 |
$api_key = 'xxxxxxxxxxxxxxxx' ; |
16 |
$usr = $username . ':' . $password ; |
17 |
$str = 'status=' .urlencode( stripslashes ( $status )). '&api_key=' . $api_key ; |
20 |
postCurl( $url , $usr , $str ); |
24 |
function postCurl( $url , $usr , $str ) { |
25 |
$ch = curl_init( $url ); |
26 |
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, 1); |
27 |
curl_setopt( $ch , CURLOPT_POST, 1); |
28 |
curl_setopt( $ch , CURLOPT_USERPWD, $usr ); |
29 |
curl_setopt( $ch , CURLOPT_POSTFIELDS, $str ); |
30 |
$data = curl_exec( $ch ); |
31 |
$Headers = curl_getinfo( $ch ); |
32 |
if ( $Headers [ 'http_code' ] == 200) { |
35 |
if ( $Headers [ 'http_code' ] == 401) { |
36 |
echo 'Username and or password not set,or api key is wrong' ; |
37 |
} elseif ( $Headers [ 'http_code' ] == 404) { |
38 |
echo 'Invalud URL for CURL request' ; |
43 |
function update_qq( $qq , $password , $content ){ |
48 |
$fields = 'content=' . $content . '&countType=&pic=&viewModel=0' ; |
52 |
curl_setopt( $curl , CURLOPT_REFERER, $referer_url ); |
53 |
curl_setopt( $curl , CURLOPT_URL, $url ); |
54 |
curl_setopt( $curl , CURLOPT_POST, 1); |
55 |
curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1); |
56 |
curl_setopt( $curl , CURLOPT_POSTFIELDS, $fields ); |
57 |
curl_setopt( $curl , CURLOPT_COOKIEJAR, $cookie_jar ); |
58 |
curl_setopt( $curl , CURLOPT_COOKIEFILE, $cookie_jar ); |
59 |
curl_setopt( $curl , CURLOPT_COOKIEJAR, $cookie_jar ); |
60 |
$loginResult = curl_exec( $curl ); |
据说腾讯的登录有时会出问题的,所以如果在使用过程出现问题,还请能安心对之,不过我现在还没出过什么问题,可能测试次数少的缘故吧。更多知识还请去上面提到的三位博主的博客吧,他们才是真正的牛人。
本文转载自转烛空间,原文地址:http:///sync-weibo。
|