Posted By

shakefon on 07/28/09


Tagged

curl php api twitter auto-follow


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

secondtwo
maxvee8
irishsk
muoto
legacye
umang_nine
palimadra
nb109
voove


Twitter Auto-Follow PHP Script


 / Published in: PHP
 

URL: http://davestevens.co.uk

Simple PHP script which allows you to specify your twitter log in details and a search term, then automatically follow users who have tweeted that term. Code is released as Open Source, use it as you wish - please leave my URL in the commented section though.

  1. <?php
  2.  
  3. // Twitter Auto-follow Script by Dave Stevens - http://davestevens.co.uk
  4.  
  5. // Set the twitter user
  6. $user = "";
  7. $pass = "";
  8.  
  9. // Set the term you want to follow (e.g. "soccer")
  10. $term = "";
  11.  
  12. // Get already followed
  13. $userApiUrl = "http://twitter.com/statuses/friends.json";
  14.  
  15. $ch = curl_init($userApiUrl);
  16. curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
  17. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  18.  
  19. $apiresponse = curl_exec($ch);
  20.  
  21. curl_close($ch);
  22.  
  23. $followed = array();
  24.  
  25. if ($apiresponse) {
  26. $json = json_decode($apiresponse);
  27. if ($json != null) {
  28. foreach ($json as $u) {
  29. $followed[] = $u->name;
  30. }
  31. }
  32. }
  33.  
  34. $userApiUrl = "http://search.twitter.com/search.json?q=" . $term . "&rpp=100";
  35.  
  36. $ch = curl_init($userApiUrl);
  37. curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
  38. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  39.  
  40. $apiresponse = curl_exec($ch);
  41.  
  42. curl_close($ch);
  43.  
  44. if ($apiresponse) {
  45. $results = json_decode($apiresponse);
  46. $count = 20;
  47.  
  48. if ($results != null) {
  49.  
  50. $resultsArr = $results->results;
  51.  
  52. if (is_array($resultsArr)) {
  53.  
  54. foreach ($resultsArr as $result) {
  55.  
  56. $from_user = $result->from_user;
  57.  
  58. if (!in_array($from_user,$followed)) {
  59.  
  60. $ch = curl_init("http://twitter.com/friendships/create/" . $from_user . ".json");
  61. curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
  62. curl_setopt($ch, CURLOPT_POST, 1);
  63. curl_setopt($ch, CURLOPT_POSTFIELDS,"follow=true");
  64. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  65.  
  66. $apiresponse = curl_exec($ch);
  67.  
  68. if ($apiresponse) {
  69. $response = json_decode($apiresponse);
  70.  
  71. if ($response != null) {
  72. if (property_exists($response,"following")) {
  73. if ($response->following === true) {
  74. echo "Now following " . $response->screen_name . "\n";
  75. } else {
  76. echo "Couldn't follow " . $response->screen_name . "\n";
  77. }
  78. } else {
  79. echo "Follow limit exceeded, skipped " . $from_user . "\n";
  80. }
  81. }
  82.  
  83. }
  84.  
  85. curl_close($ch);
  86.  
  87. } else {
  88. echo "Already following " . $from_user . "\n";
  89. }
  90.  
  91. }
  92.  
  93. }
  94.  
  95. }
  96.  
  97.  
  98. }
  99.  
  100. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jonathonmaguire on December 27, 2009

Hey I have modified the php code just a bit to enable a person to add their own details on a webpage and it will process it on the same webpage.

Here is my code:

Twitter Follow Script - Jonathon Maguire

Twitter Follow Script

div { border: .2em dotted #900; }

border {

border-width: .2em; border-style: solid; border-color: red; }

Username:

Password:

Search Term

You need to login to post a comment.