Twitter Tool: PHP Auto-Follow Script

UPDATE: This is no longer supported by Twitter.

I found this script on webupd8.org that will help you jump-start your Twitter campaigns. You can run it on any PHP server as a standalone file. What it does is search for anyone who mentions a relevant term. Just copy and paste the code below and change the following:

  • enter_your_twitter_username_here // Example: eCommerceCircle
  • enter_your_twitter_password_here // Example: imnottellingyou
  • enter_the_search_term_to_follow_here // Example: iPod Touch

You can upload to your server and just load the page from time to time or set up a cron job to do it automatically. Keep in mind that Twitter has follower/following ratios in place and that at most you cannot follow more than 1.5 times the amount of people following you as well as a 1,000 follower a day limit.

[code lang=”php”]<?php
// Twitter Auto-follow Script by Dave Stevens – http://davestevens.co.uk

$user = "enter_your_twitter_username_here";
$pass = "enter_your_twitter_password_here";

$term = "enter_the_search_term_to_follow_here";

$userApiUrl = "http://twitter.com/statuses/friends.json";

$ch = curl_init($userApiUrl);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$apiresponse = curl_exec($ch);
curl_close($ch);
$followed = array();

if ($apiresponse) {
$json = json_decode($apiresponse);
if ($json != null) {
foreach ($json as $u) {
$followed[] = $u->name;
}
}
}

$userApiUrl = "http://search.twitter.com/search.json?q=" . $term . "&rpp=100";
$ch = curl_init($userApiUrl);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$apiresponse = curl_exec($ch);
curl_close($ch);

if ($apiresponse) {
$results = json_decode($apiresponse);
$count = 20;
if ($results != null) {
$resultsArr = $results->results;
if (is_array($resultsArr)) {
foreach ($resultsArr as $result) {
$from_user = $result->from_user;
if (!in_array($from_user,$followed)) {
$ch = curl_init("http://twitter.com/friendships/create/" . $from_user . ".json");
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,"follow=true");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$apiresponse = curl_exec($ch);

if ($apiresponse) {
$response = json_decode($apiresponse);
if ($response != null) {
if (property_exists($response,"following")) {
if ($response->following === true) {
echo "Now following " . $response->screen_name . "\n";
} else {
echo "Couldn’t follow " . $response->screen_name . "\n";
}
} else {
echo "Follow limit exceeded, skipped " . $from_user . "\n";
}
}
}
curl_close($ch);
} else {
echo "Already following " . $from_user . "\n";
}
}
}
}
}
?>[/code]

Comments (4)

Comments are closed.