Reputation: 103
I tried to get follower list using code below. This code is successful for accounts that have hundreds of users but does not get all followers with account which have thousands of users.What can I do for enhance this code?
<?php
header('Content-type: text/html; charset=utf8');
require_once('twitteroauth/twitteroauth.php');
// consumer ve access
$consumerkey = "CONSUMERKEY";
$consumersecret = "CONSUMERSECRET";
$accesstoken = "ACCESSTOKEN";
$accesstokensecret = "ACCESSTOKENSECRET";
// sınıfı başlatalım
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
// Empty array that will be used to store followers.
$profiles = array();
// Get the ids of all followers. Max. 5000
$sc_name = 'mgocenoglu';
$ids = $connection->get("https://api.twitter.com/1.1/followers/ids.json?screen_name=$sc_name");
//print_r($ids->ids);
// Chunk the ids in to arrays of 100.
$ids_arrays = array_chunk($ids->ids, 100);
//print_r($ids_arrays);
// Loop through each array of 100 ids.
$i=1;
foreach($ids_arrays as $implode) {
// Perform a lookup for each chunk of 100 ids.
$user_ids=implode(',', $implode);
//echo $user_ids."<br>";
$results = $connection->get("https://api.twitter.com/1.1/users/lookup.json?user_id=$user_ids");
// Loop through each profile result.
foreach($results as $profile) {
//echo $i++."-".$profile->name." ".$profile->followers_count."<br>";
$profiles[$profile->name] = $profile;
}
}
//Sorting profiles according to followers count
$sortArray = array();
foreach($profiles as $person){
foreach($person as $key=>$value){
if(!isset($sortArray[$key])){
$sortArray[$key] = array();
}
$sortArray[$key][] = $value;
}
}
$orderby = "followers_count"; //change this to whatever key you want from the array
array_multisort($sortArray[$orderby],SORT_DESC,$profiles);
?>
<html><body><table border=1>
<?php
foreach($profiles as $profile) {
echo "<tr><td>".$i++."</td><td>".$profile->name."</td><td>".$profile->screen_name."</td><td>".$profile->followers_count."</td></tr>";
}
?>
</table></body></html>
Upvotes: 1
Views: 1664