Monday, 15 September 2014

Get Follower List Using PHP and Twitter REST API -



Get Follower List Using PHP and Twitter REST API -

i tried follower list using code below. code successful accounts have hundreds of users not followers business relationship have thousands of users.what can enhance 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 used store followers. $profiles = array(); // ids of 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 ids in 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 $implode) { // perform lookup 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 $profile) { //echo $i++."-".$profile->name." ".$profile->followers_count."<br>"; $profiles[$profile->name] = $profile; } } //sorting profiles according followers count $sortarray = array(); foreach($profiles $person){ foreach($person $key=>$value){ if(!isset($sortarray[$key])){ $sortarray[$key] = array(); } $sortarray[$key][] = $value; } } $orderby = "followers_count"; //change whatever key want array array_multisort($sortarray[$orderby],sort_desc,$profiles); ?> <html><body><table border=1> <?php foreach($profiles $profile) { echo "<tr><td>".$i++."</td><td>".$profile->name."</td><td>".$profile->screen_name."</td><td>".$profile->followers_count."</td></tr>"; } ?> </table></body></html>

// ids of followers. max. 5000

php twitter

No comments:

Post a Comment