At first, hello everybody!
I want to make a php script which autofollow other users on twitter.
The userinformation like the twitter username password or the search tearm will be saved in a database and read out.
Here are is my code so far, but won't work correct:
Autfollow.php
$con = mysql_connect('host', 'user', 'pw') or exit(mysql_error());
mysql_select_db('db', $con) or exit(mysql_error());
$sql = 'SELECT * FROM autofollow WHERE `active` =1 AND `done` =0';
$sqlresult = mysql_query($sql);
echo mysql_error();
while ($row = mysql_fetch_array($sqlresult))
{
$user=$row{'tuser'} ;
$pass=$row{'tpass'};
$counter=$row{'max'};
$term=$row{'term'};
include "autofollowuser.php";
$sql = 'UPDATE autofollow SET `done` =1 WHERE `tuser` =\''.$user.'\'';
mysql_query($sql);
}
This script works fine for me to read out all users from database.
Here is the autfollowuser.php:
<?php
$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);
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)&&$counter>0) {
$counter--;
$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";
}
}
}
}
}
?>
The problem is the first two users in database will work, but after that it won't work.
The Twitter accounts are correct!
I think there is a problem with this include, but I am not really good at php.
Hope you can find the bug in it!
I looking forward to your answers.