How to change Twitter's status with php and Curl without oAuth


/ Published in: PHP
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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. }

URL: http://www.barattalo.it/2010/09/09/how-to-change-twitter-status-with-php-and-curl-without-oauth/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.