分享

利用 PHP 和 Follow5 同步腾讯微博 | SimonLeung's Blog

 看见就非常 2012-09-26

如果你是个微博同步控,这篇文章可以助你一臂之力。首先 Follow5 可以用来同步几乎所有微博,不过大名鼎鼎的腾讯微博不可以,那么本文就此应运而生。编程语言用的PHP,运行在开启了 curl 的 PHP 服务器上,所以首先要确定你的服务器开启了 curl。本段代码主要是实现发微博到 Follow5 和腾讯微博,利用 Follow5 的五步大同功能,就不必再发其它微博上了。

本文主要参考了三篇文章:腾讯微博API同步消息到腾讯微博发布一个Follow5的API PHP类 。因此如果你对本文有任何不理解的地方,可以详细阅读以上文章。

闲话少说,发表微博到 Follow5,你还得需要一个 Follow5 的 key,在这里我要表扬 Follow5 办事效率高,一个晚上就将 key 发给我了,太高兴了。以下就是代码,如果因为代码损害了原博主,还请告知。本文对 PHP 和 python 一窍不通,所以几乎都是啃大家的代码然后加以组合。

01 <?php
02 if ($_POST['status']) {
03  $status $_POST['status'];
04  update_follow5('username''password'$status);
05  
06  //此处的username是你的腾讯微博用户名,而不是你的Q号码
07  update_qq('username''password'$status);    
08  
09  //跳回到发表微博的界面
10  header("location: xxxxx.html");
11 }
12  
13 function update_follow5($username$password$status){
14  //这个api_key需要自己去向Follow5申请
15  $api_key 'xxxxxxxxxxxxxxxx';
16  $usr $username.':'.$password;
17  $str 'status='.urlencode(stripslashes($status)).'&api_key='.$api_key;
19  
20  postCurl($url$usr$str);
21 }
22  
23 //此处代码参考http://www./349.html
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) {
33  return $data;
34  else {
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';
39  }
40  }
41 }
42  
43 function update_qq($qq$password$content){
44  //省却代码无数,补充代码请参看http://www./archives/tencent-microblog-api/
45  
46  //增加这第五次是用来发表腾讯微博的
47  //改装来自于http:///archives/279.html
48  $fields 'content='.$content.'&countType=&pic=&viewModel=0';
51  $curl = curl_init();
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);
61  curl_close($curl);
62  
63 unlink($cookie_jar);
64 }
65 ?>

 

据说腾讯的登录有时会出问题的,所以如果在使用过程出现问题,还请能安心对之,不过我现在还没出过什么问题,可能测试次数少的缘故吧。更多知识还请去上面提到的三位博主的博客吧,他们才是真正的牛人。

本文转载自转烛空间,原文地址:http:///sync-weibo

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约