如何使用php和Curl更改Twitter的状态而不使用oAuth

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用php和Curl更改Twitter的状态而不使用oAuth相关的知识,希望对你有一定的参考价值。

Since the 31 of august 2010, twitter made its API more secure, stopping basic authentication calls.
So, if you used basic authentication you have to change your code and implement oAuth authentication model or you can follow this link and found a mini spider that does the work.
  1. function twitterSetStatus($user,$pwd,$status) {
  2. if (!function_exists("curl_init")) die("twitterSetStatus needs CURL module, please install CURL on your php.");
  3. $ch = curl_init();
  4.  
  5. // -------------------------------------------------------
  6. // get login form and parse it
  7. curl_setopt($ch, CURLOPT_URL, "https://mobile.twitter.com/session/new");
  8. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  9. curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  10. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  12. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  13. curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies.txt");
  14. curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies.txt");
  15. curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (Khtml, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3 ");
  16. $page = curl_exec($ch);
  17. $page = stristr($page, "<div class='signup-body'>");
  18. preg_match("/form action="(.*?)"/", $page, $action);
  19. preg_match("/input name="authenticity_token" type="hidden" value="(.*?)"/", $page, $authenticity_token);
  20.  
  21. // -------------------------------------------------------
  22. // make login and get home page
  23. $strpost = "authenticity_token=".urlencode($authenticity_token[1])."&username=".urlencode($user)."&password=".urlencode($pwd);
  24. curl_setopt($ch, CURLOPT_URL, $action[1]);
  25. curl_setopt($ch, CURLOPT_POSTFIELDS, $strpost);
  26. $page = curl_exec($ch);
  27. // check if login was ok
  28. preg_match("/<div class="warning">(.*?)</div>/", $page, $warning);
  29. if (isset($warning[1])) return $warning[1];
  30. $page = stristr($page,"<div class='tweetbox'>");
  31. preg_match("/form action="(.*?)"/", $page, $action);
  32. preg_match("/input name="authenticity_token" type="hidden" value="(.*?)"/", $page, $authenticity_token);
  33.  
  34. // -------------------------------------------------------
  35. // send status update
  36. $strpost = "authenticity_token=".urlencode($authenticity_token[1]);
  37. $tweet['display_coordinates']='';
  38. $tweet['in_reply_to_status_id']='';
  39. $tweet['lat']='';
  40. $tweet['long']='';
  41. $tweet['place_id']='';
  42. $tweet['text']=$status;
  43. $ar = array("authenticity_token" => $authenticity_token[1], "tweet"=>$tweet);
  44. $data = http_build_query($ar);
  45. curl_setopt($ch, CURLOPT_URL, $action[1]);
  46. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  47. $page = curl_exec($ch);
  48.  
  49. return true;
  50. }

以上是关于如何使用php和Curl更改Twitter的状态而不使用oAuth的主要内容,如果未能解决你的问题,请参考以下文章

无法使用 Twitter API、PHP 和 curl 发布图像和文本?媒体/上传和状态/更新

当有人使用twitter api和php发布推文时如何获得通知

使用PHP定制Twitter徽章

最近的Twitter状态

PHP 使用PHP和XML检索Twitter状态

一个使用Curl发布(GET)到Twitter的简单函数