Posted By

jonathonmaguire on 12/27/09


Tagged

php html script twitter follow


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

spf13
vehler
zlojkashtan
maxvee8


Twitter Follow Script Modified


 / Published in: PHP
 

URL: http://www.davestevens.co.uk

Modified version of the script created by Dave Stevens, Enables the user to input their credentials on a webpage rather than having to change the actual php script code.

Changes: Changed $user,$pass,$term

This is a modified script, the original can be found here: http://snipplr.com/view/17595/twitter-autofollow-php-script/

  1. <html>
  2. <head>
  3. <title>Twitter Follow Script - Jonathon Maguire</title>
  4. </head>
  5. <body>
  6. <center>
  7. <u><h1><i>Twitter Follow Script</i></h1></u>
  8. <style>
  9. div {
  10. border: .2em dotted #900;
  11. }
  12. #border {
  13. border-width: .2em;
  14. border-style: solid;
  15. border-color: red;
  16. }
  17. </style>
  18. <form method="post" action="">
  19. <div id="border">
  20. <p><u>Username:</u></p>
  21. <input type="text" name="username" value="">
  22. <br><p><u>Password:</u></p>
  23. <input type="password" name="password" value="">
  24. <br><p><u>Search Term</u></p>
  25. <input type="text" name="term" value=""><br>
  26. <input type="submit" name="submit" value="submit">
  27. </div>
  28. </center>
  29.  
  30. </body>
  31. </html>
  32. <?php
  33.  
  34. // Twitter Auto-follow Script by Dave Stevens - http://davestevens.co.uk, Modified by Jonathon Maguire, http://www.unknowntruth.net
  35.  
  36. if(isset($_POST['submit']))
  37. {
  38. // Set the twitter user
  39. $user = $_POST['username'];
  40. $pass = $_POST['password'];
  41.  
  42. // Set the term you want to follow (e.g. "soccer")
  43. $term = $_POST['term'];
  44.  
  45. // Get already followed
  46. $userApiUrl = "http://twitter.com/statuses/friends.json";
  47.  
  48. if(isset($_POST['submit']))
  49. $ch = curl_init($userApiUrl);
  50. curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  52.  
  53. $apiresponse = curl_exec($ch);
  54.  
  55. curl_close($ch);
  56.  
  57. $followed = array();
  58.  
  59. if ($apiresponse) {
  60. $json = json_decode($apiresponse);
  61. if ($json != null) {
  62. foreach ($json as $u) {
  63. $followed[] = $u->name;
  64. }
  65. }
  66. }
  67.  
  68. $userApiUrl = "http://search.twitter.com/search.json?q=" . $term . "&rpp=100";
  69.  
  70. $ch = curl_init($userApiUrl);
  71. curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
  72. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  73.  
  74. $apiresponse = curl_exec($ch);
  75.  
  76. curl_close($ch);
  77.  
  78. if ($apiresponse) {
  79. $results = json_decode($apiresponse);
  80. $count = 90;
  81.  
  82. if ($results != null) {
  83.  
  84. $resultsArr = $results->results;
  85.  
  86. if (is_array($resultsArr)) {
  87.  
  88. foreach ($resultsArr as $result) {
  89.  
  90. $from_user = $result->from_user;
  91.  
  92. if (!in_array($from_user,$followed)) {
  93.  
  94. $ch = curl_init("http://twitter.com/friendships/create/" . $from_user . ".json");
  95. curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
  96. curl_setopt($ch, CURLOPT_POST, 1);
  97. curl_setopt($ch, CURLOPT_POSTFIELDS,"follow=true");
  98. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  99.  
  100. $apiresponse = curl_exec($ch);
  101.  
  102. if ($apiresponse) {
  103. $response = json_decode($apiresponse);
  104.  
  105. if ($response != null) {
  106. if (property_exists($response,"following")) {
  107. if ($response->following === true) {
  108. echo "Now following " . $response->screen_name . "\n";
  109. } else {
  110. echo "Couldn't follow " . $response->screen_name . "\n";
  111. }
  112. } else {
  113. echo "Follow limit exceeded, skipped " . $from_user . "\n";
  114. }
  115. }
  116.  
  117. }
  118.  
  119. curl_close($ch);
  120.  
  121. } else {
  122. echo "Already following " . $from_user . "\n";
  123. }
  124.  
  125. }
  126.  
  127. }
  128.  
  129. }
  130.  
  131.  
  132. }
  133. }
  134.  
  135. ?>

Report this snippet  

You need to login to post a comment.