How to get a list of followers or following?

pauljbeaton
@pauljbeaton Paul Beaton

When I set up my Foursquare account, I was able to send invites to all the people that I follow that have a Foursquare account. I'm looking for a way to get a list of people I follow. How is that done?

2 years 39 weeks ago

Replies

moneymasternow1
@moneymasternow1 Teresa

Did you learn how to do this yet?

2 years 36 weeks ago
episod
@episod Taylor Singletary

If you just need the user ids of the users you are following or who follow you, then it's usually a single API call. If you need the screen_names and other biographical data, you'll need to chain API requests together.

The methods you are looking for are:
* GET friends/ids and GET followers/ids for getting the ids
* GET users/lookup to "hydrate" the user_ids into full user objects

2 years 36 weeks ago
HypoGal101
@HypoGal101 HypoGal 101

Thank You

1 year 37 weeks ago
rohitfantasy
@rohitfantasy Rohit Dhiman

<?php
$trends_url = "http://api.twitter.com/1/statuses/followers/rohitfantasy.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $trends_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
foreach($response as $friends){
$thumb = $friends['profile_image_url'];
$url = $friends['screen_name'];
$name = $friends['name'];

?>
<a title="<?php echo $name;?>" href="http://www.twitter.com/<?php echo $url;?>">

" border="0" alt="" width="40" />

<?php
}

2 years 18 weeks ago
rohitfantasy
@rohitfantasy Rohit Dhiman

here is the code i am using for getting the list of followers...

2 years 18 weeks ago
shivaavail
@shivaavail shiva shankar

How can I send messages after getting followers????

1 year 42 weeks ago
caoptson
@caoptson Cao P.T Sơn

i always get response

array
'request' => string '/1/statuses/followers/792875406.json' (length=36)
'error' => string 'Not authorized' (length=14)

1 year 8 weeks ago
rahkes50anug
@rahkes50anug BATHINA.GUNASEKHAR

not working

13 weeks 2 days ago
ixorajavanica
@ixorajavanica Ixorɑ Jɑvɑnicɑ ♚♕

How to get a list of followers or following?

1 year 51 weeks ago
episod
@episod Taylor Singletary

The methods you are looking for are:
* GET friends/ids and GET followers/ids for getting the ids
* GET users/lookup to "hydrate" the user_ids into full user objects

1 year 51 weeks ago
RamChandraDash
@RamChandraDash R@m Chandra Dash

how to use this code in my profile.. please guide me..

48 weeks 5 days ago
sentimentoeu
@sentimentoeu sentimento eu
  • GET / IDS amigos e seguidores GET / IDS para obter o ids
  • GET usuários / pesquisa para "hidratar" os user_ids em objetos totais de usuário
1 year 48 weeks ago
ZKM
@ZKM Zach Schneider

updated your print statement

  1. <?php 
  2. $trends_url = "http://api.twitter.com/1/statuses/followers/zkm.json";
  3. $ch = curl_init();
  4. curl_setopt($ch, CURLOPT_URL, $trends_url);
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  6. $curlout = curl_exec($ch);
  7. curl_close($ch);
  8. $response = json_decode($curlout, true);
  9. foreach($response as $friends){
  10.   $thumb = $friends['profile_image_url'];
  11.   $url = $friends['screen_name'];   
  12.    $name = $friends['name'];
  13.  
  14.    print "<a title='" . $name . "' href='http://www.twitter.com/" . $url . "'>" . "<img src='" . $thumb . "' /></a>";
  15. }
1 year 46 weeks ago
designlayerz
@designlayerz Design Layerz

i just tried this code and its telling me that curl_init() has not been declared

1 year 31 weeks ago
rahkes50anug
@rahkes50anug BATHINA.GUNASEKHAR

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\abc\F14\user\tw.php on line 97

13 weeks 2 days ago
punkmixa
@punkmixa Sum

also u can use the user_id

  1.   $trends_url = "http://api.twitter.com/1/statuses/followers/226841244.json";
1 year 38 weeks ago
gaurav00137
@gaurav00137 Gaurav Thakur

its working for me

1 year 28 weeks ago
RajeshreddyM1
@RajeshreddyM1 Rajessh Reddy M

it's only retrieving the first 100 followers, how should I get the remaining in addition to the first 100

51 weeks 1 day ago
Vijay18122862
@Vijay18122862 Vijay

Please anybody have full source to how to get name and followers.
eg. i am giving my twitter id and password it will display a no.of followers and name of current login user.

1 year 37 weeks ago
mtasuandi
@mtasuandi MTΛ Suandi

Hei vijay, the code given by @rohitfantasy and @ZKM is correct way to get username by followers. if you just want to display username of your followers just echo $url.

1 year 36 weeks ago
rogsonline
@rogsonline Roegan

Hmm just wondering if it's possible to get "mutual following"? It's like fetching only the users you follow, and at the same time, they follow you back?

1 year 34 weeks ago
ironyzm
@ironyzm bud2 ironyzm

any one know, i use this and work on my xampp

  1.   //Twitter Follower
  2.  $twit = file_get_contents('http://twitter.com/users/show/'.$user['twitter'].'.xml');
  3.  $begin = '<followers_count>'; $end = '</followers_count>';
  4.  $page = $twit;
  5.  $parts = explode($begin,$page);
  6.     $page = $parts[1];
  7.  $parts = explode($end,$page);
  8.   $tcount = $parts[0];
  9.    if($tcount == '') { $tcount = 0; }
  10.    $tcount = number_format($tcount);
  11.   $Aipolow = ''.$tcount.' Follower';
  12. echo '<li>Twitter: '.$Aipolow.'</li>';

but when i use it on my site the count still 0

1 year 33 weeks ago
Earth2Eddie
@Earth2Eddie Earth 2 Eddie

I know to get more than the small list you have to use authentication. Does the code change though if you want the full list of followers?

1 year 32 weeks ago
jaiprakash_yada
@jaiprakash_yada Jai Prakash

sss

1 year 24 weeks ago
revolt1857
@revolt1857 vishal mahajan

how to get the name of followers in ipython using twython

1 year 15 weeks ago
806499632
@806499632 张科勇

获取twitter上的好友列表

1 year 9 weeks ago
kejoff
@kejoff Kévin Joffard

I'm stucked to get the famous 5000 IDs. I got only 300. Could somebody help me on this issue. Here is my code

1st file : getfollowers.php

  1. <?php
  2.  
  3. require_once('TwitterAPIExchange.php');
  4.  
  5. $consumerKey = 'key';
  6. $consumerKeySecret = 'secret_key';
  7. $accessToken = 'token';
  8. $accessTokenSecret = 'token_secret';
  9.  
  10. $settings = array(
  11.   'oauth_access_token' => $accessToken,
  12.   'oauth_access_token_secret' => $accessTokenSecret,
  13.   'consumer_key' => $consumerKey,
  14.   'consumer_secret' => $consumerKeySecret
  15. );
  16.  
  17. $i = 0;
  18. $cursor = -1;
  19.  
  20. do {
  21.   $url = 'https://api.twitter.com/1.1/followers/list.json';
  22.   $getfield = '?cursor='.$cursor.'&screen_name=peugeot&skip_status=true&include_user_entities=false';
  23.   $requestMethod = 'GET';
  24.   $twitter = new TwitterAPIExchange($settings);
  25.   $response = $twitter->setGetfield($getfield)
  26.                       ->buildOauth($url, $requestMethod)
  27.                       ->performRequest();
  28.  
  29.   $response = json_decode($response, true);
  30.   $errors = $response["errors"];
  31.  
  32.   if (!empty($errors)) {
  33.     foreach($errors as $error){
  34.       $code = $error['code'];
  35.       $msg = $error['message'];
  36.       echo "<br><br>Error " . $code . ": " . $msg;
  37.     }
  38.     $cursor = 0;
  39.   }
  40.   else {
  41.     $users = $response['users'];
  42.     foreach($users as $user){
  43.       $thumb = $user['profile_image_url'];
  44.       $url = $user['screen_name'];   
  45.       $name = $user['name'];
  46.       echo "<a title='" . $name . "' href='http://www.twitter.com/" . $url . "'>" . "<img src='" . $thumb . "' /></a>";
  47.       $i++;
  48.     }
  49.     $cursor = $response["next_cursor"];
  50.   }
  51. }
  52. while ( $cursor != 0 );
  53.  
  54. if (!empty($users)) {
  55.   echo '<br><br>Total: ' . $i;
  56. }
  57.  
  58. ?>

2nd file : TwitterAPIexchange

  1. class TwitterAPIExchange 
  2. {
  3.     private $oauth_access_token;
  4.     private $oauth_access_token_secret;
  5.     private $consumer_key;
  6.     private $consumer_secret;
  7.     private $postfields;
  8.     private $getfield;
  9.     protected $oauth;
  10.     public $url;
  11.  
  12.     /
  13.      * Create the API access object. Requires an array of settings::
  14.      * oauth access token, oauth access token secret, consumer key, consumer secret
  15.      * These are all available by creating your own application on dev.twitter.com
  16.      * Requires the cURL library
  17.      * 
  18.      * @param array $settings
  19.      */
  20.     public function __construct(array $settings)
  21.     {
  22.         if (!in_array('curl', get_loaded_extensions())) 
  23.         {
  24.             throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html');
  25.         }
  26.  
  27.         if (!isset($settings['oauth_access_token'])
  28.             || !isset($settings['oauth_access_token_secret'])
  29.             || !isset($settings['consumer_key'])
  30.             || !isset($settings['consumer_secret']))
  31.         {
  32.             throw new Exception('Make sure you are passing in the correct parameters');
  33.         }
  34.  
  35.         $this->oauth_access_token = $settings['oauth_access_token'];
  36.         $this->oauth_access_token_secret = $settings['oauth_access_token_secret'];
  37.         $this->consumer_key = $settings['consumer_key'];
  38.         $this->consumer_secret = $settings['consumer_secret'];
  39.     }
  40.  
  41.     /
  42.      * Set postfields array, example: array('screen_name' => 'J7mbo')
  43.      * 
  44.      * @param array $array Array of parameters to send to API
  45.      * @return \TwitterAPIExchange Instance of self for method chaining
  46.      */
  47.     public function setPostfields(array $array)
  48.     {
  49.         if (!is_null($this->getGetfield())) 
  50.         { 
  51.             throw new Exception('You can only choose get OR post fields.'); 
  52.         }
  53.         $this->postfields = $array;
  54.         return $this;
  55.     }
  56.  
  57.     /
  58.      * Set getfield string, example: '?screen_name=J7mbo'
  59.      * 
  60.      * @param string $string Get key and value pairs as string
  61.      * @return \TwitterAPIExchange Instance of self for method chaining
  62.      */
  63.     public function setGetfield($string)
  64.     {
  65.         if (!is_null($this->getPostfields())) 
  66.         { 
  67.             throw new Exception('You can only choose get OR post fields.'); 
  68.         }
  69.  
  70.         $this->getfield = $string;
  71.         return $this;
  72.     }
  73.  
  74.     /
  75.      * Get getfield string (simple getter)
  76.      * 
  77.      * @return string $this->getfields
  78.      */
  79.     public function getGetfield()
  80.     {
  81.         return $this->getfield;
  82.     }
  83.  
  84.     /
  85.      * Get postfields array (simple getter)
  86.      * 
  87.      * @return array $this->postfields
  88.      */
  89.     public function getPostfields()
  90.     {
  91.         return $this->postfields;
  92.     }
  93.  
  94.     /
  95.      * Build the Oauth object using params set in construct and additionals
  96.      * passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1
  97.      * 
  98.      * @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json
  99.      * @param string $requestMethod Either POST or GET
  100.      * @return \TwitterAPIExchange Instance of self for method chaining
  101.      */
  102.     public function buildOauth($url, $requestMethod)
  103.     {
  104.         if (strtolower($requestMethod) !== 'post' && strtolower($requestMethod) !== 'get')
  105.         {
  106.             throw new Exception('Request method must be either POST or GET');
  107.         }
  108.  
  109.         $consumer_key = $this->consumer_key;
  110.         $consumer_secret = $this->consumer_secret;
  111.         $oauth_access_token = $this->oauth_access_token;
  112.         $oauth_access_token_secret = $this->oauth_access_token_secret;
  113.  
  114.         $oauth = array( 
  115.             'oauth_consumer_key' => $consumer_key,
  116.             'oauth_nonce' => time(),
  117.             'oauth_signature_method' => 'HMAC-SHA1',
  118.             'oauth_token' => $oauth_access_token,
  119.             'oauth_timestamp' => time(),
  120.             'oauth_version' => '1.0'
  121.         );
  122.  
  123.         $getfield = $this->getGetfield();
  124.  
  125.         if (!is_null($getfield))
  126.         {
  127.             $getfields = str_replace('?', '', explode('&', $getfield));
  128.             foreach ($getfields as $g)
  129.             {
  130.                 $split = explode('=', $g);
  131.                 $oauth[$split[0]] = $split[1];
  132.             }
  133.         }
  134.  
  135.         $base_info = $this->buildBaseString($url, $requestMethod, $oauth);
  136.         $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
  137.         $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
  138.         $oauth['oauth_signature'] = $oauth_signature;
  139.  
  140.         $this->url = $url;
  141.         $this->oauth = $oauth;
  142.  
  143.         return $this;
  144.     }
  145.  
  146.     /
  147.      * Perform the acual data retrieval from the API
  148.      * 
  149.      * @param boolean optional $return If true, returns data. 
  150.      * @return json If $return param is true, returns json data.
  151.      */
  152.     public function performRequest($return = true)
  153.     {
  154.         if (!is_bool($return)) 
  155.         { 
  156.             throw new Exception('performRequest parameter must be true or false'); 
  157.         }
  158.  
  159.         $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:');
  160.  
  161.         $getfield = $this->getGetfield();
  162.         $postfields = $this->getPostfields();
  163.  
  164.         $options = array( 
  165.             CURLOPT_HTTPHEADER => $header,
  166.             CURLOPT_HEADER => false,
  167.             CURLOPT_URL => $this->url,
  168.             CURLOPT_RETURNTRANSFER => true,
  169.             CURLOPT_SSL_VERIFYPEER => false
  170.         );
  171.  
  172.         if (!is_null($postfields))
  173.         {
  174.             $options[CURLOPT_POSTFIELDS] = $postfields;
  175.         }
  176.         else
  177.         {
  178.             if ($getfield !== '')
  179.             {
  180.                 $options[CURLOPT_URL] .= $getfield;
  181.             }
  182.         }
  183.  
  184.         $feed = curl_init();
  185.         curl_setopt_array($feed, $options);
  186.         $json = curl_exec($feed);
  187.         curl_close($feed);
  188.  
  189.         if ($return) { return $json; }
  190.     }
  191.  
  192.     /
  193.      * Private method to generate the base string used by cURL
  194.      * 
  195.      * @param string $baseURI
  196.      * @param string $method
  197.      * @param string $params
  198.      * @return string Built base string
  199.      */
  200.     private function buildBaseString($baseURI, $method, $params) 
  201.     {
  202.         $return = array();
  203.         ksort($params);
  204.  
  205.         foreach($params as $key=>$value)
  206.         {
  207.             $return[] = "$key=" . $value;
  208.         }
  209.  
  210.         return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return)); 
  211.     }
  212.  
  213.     /**
  214.      * Private method to generate authorization header used by cURL
  215.      * 
  216.      * @param array $oauth Array of oauth data generated by buildOauth()
  217.      * @return string $return Header used by cURL for request
  218.      */    
  219.     private function buildAuthorizationHeader($oauth) 
  220.     {
  221.         $return = 'Authorization: OAuth ';
  222.         $values = array();
  223.  
  224.         foreach($oauth as $key => $value)
  225.         {
  226.             $values[] = "$key=\"" . rawurlencode($value) . "\"";
  227.         }
  228.         $return .= implode(', ', $values);
  229.         return $return;
  230.     }
  231.  
  232. }

Thanks for your help

47 weeks 2 hours ago
darkpoltweeter
@darkpoltweeter Dark Politricks RT

Hi since moving to API 1.1 I keep getting the following error message when I try to get a list of my followers.

{"errors":[{"message":"Bad Authentication data","code":215}]}

Basically the script is using the same Twitter / oAuth classes as most Wordpress plugins do (including my own) and I have changed line 29 in the Twitter class to:

/* Set up the API root URL. */
public $host = "https://api.twitter.com/1.1/";

Which resolved the issue in my own WordPress plugins. However I just cannot seem to get a list of my followers back even if I can send tweets out.

My scripts are run from CRON jobs and I connect to Twitter with my application consumer key/secret and access key/secret and the authentication message comes back that I am logged in.

I then try and access the URL that should return your followers in the new Twitter 1.1 API e.g

https://api.twitter.com/1.1/followers/list.json?screen_name=mytwitteraccount

However even though I am logged in I cannot get a response.

I am using the inbuilt Twitter method http which uses CURL and passes your consumer / access keys and secrets along with all requests.

$response =$oauth->http("https://api.twitter.com/1.1/followers/list.json?screen_name=mytwitteraccount","GET");

However even though the oauth class is telling me that the credentials for my screen name are valid, when I try to make a GET request to get the JSON for my followers list I get back the Bad Authentication 215 error message:

The full code is below:

  1. // a simple debug statement echo/print_r
  2. ShowDebug("load twitter");
  3.  
  4. // include the twitter class which also includes the oauth class
  5. require_once('twitter.class.php' );
  6.  
  7. ShowDebug("loaded twitter class");
  8.  
  9. // Twitter settings - these are fake so don't try and use them!
  10. $consumersecret = "ghweweLGhQxA2zGiHrUFW6X8HTdsJPOrau0fw24veUU";
  11. $consumerkey = "1pRwT3gNcmmnLKnrp9g";
  12.  
  13. $accessToken        = "112534562-Jj5QLN7XpRNgKjHi289DvBWDrmt1jxX2Wink5tTvbRe";
  14. $accessTokenSecret = "lbXdFtGttkjEGpmgGqsAACftLfseCvemxfNPKrCeTmENtUOk";
  15.  
  16. ShowDebug("connect with access token = " . $accessToken . " and secret = " . $accessTokenSecret);
  17.  
  18. // pass my keys
  19. $oauth = new TwitterOAuth($consumerkey, $consumersecret, $accessToken, $accessTokenSecret);
  20.  
  21. ShowDebug("VERIFY ACCOUNT WITH OAUTH");
  22.  
  23. // Send an API request to verify credentials
  24. $credentials = $oauth->get("account/verify_credentials");
  25.  
  26. // a print_r shows me all my details here - so I am verified!
  27. print_r($credentials);
  28.  
  29. // so I should be logged in now
  30. if(!empty($credentials->screen_name))
  31. {
  32.   ShowDebug( "Connected as @" . $credentials->screen_name);
  33.  
  34.     ShowDebug("check for new followers");
  35.  
  36.  
  37.   $followers_url = "https://api.twitter.com/1.1/followers/list.json?screen_name=myscreenname";
  38.  
  39.     $response = $oauth->http($followers_url,"GET");
  40.  
  41.   ShowDebug($response);
  42.   ShowDebug("status is " . $oauth-&gt;http_code);
  43. }

And the debug I get back is the following:

load twitter
load oauth
do class
OAuthToken loaded
loaded twitter class
connect with access token = 112534562-Jj5QLN7XpRNgKjHi289DvBWDrmt1jxX2Wink5tTvbRe and secret = lbXdFtGttkjEGpmgGqsAACftLfseCvemxfNPKrCeTmENtUOk
VERIFY ACCOUNT WITH OAUTH
Connected as @MyTwitterAccount
check for new followers
{"errors":[{"message":"Bad Authentication data","code":215}]}
status is 400

If anyone can help me then please let me have your knowledge! Twitter so far haven't helped at all.

And before anyone asks

Yes I have an application set up for my Twitter account to carry out these requests.
Yes I have double checked my consumer and access keys.
The URL to get the follower list was copied direct from the twitter developer site.

Also if instead of trying to get a list of followers back I run this code instead

  1. $replymsg = "a test message";
  2.  
  3.      $res = $oauth->post(
  4.                 'statuses/update', 
  5.               array(
  6.                  'status' => $replymsg,
  7.                     'source' => 'Strictly Tweetbot'
  8.              )
  9.           );
  10.  
  11.  
  12.  
  13.       if(isset($res->error) && !empty($res->error)){
  14.  
  15.           ShowDebug("An error occurred posting $replymsg to $name - " . $res->error);    
  16.  
  17.       }else{
  18.  
  19.            ShowDebug("TWEET SENT OK");
  20.  
  21.          $success = true;
  22.        }

I can send a tweet. Therefore I am authenticated properly.

Any help would be much appreciated!

45 weeks 10 hours ago
darkpoltweeter
@darkpoltweeter Dark Politricks RT

Dont worry I have fixed it all

A simple change from a file_get_contents to

$response = $oauth->get($followers_url);

and then either loop through the JSON or convert it to a string with json_encode and a quick regex will give you just screen names without all the rest e.g

$body = json_encode($response);

preg_match_all('@"screen_name":"([\s\S]+?)",@i',$body,$matches,PREG_SET_ORDER);

then a loop through the matches with a send tweet and hey presto - fixed!

I wish I had known about this before tonight - I have spent hours fixing all this! Plus all my old yahoo pipes which were taking in RSS feeds of statuses for Retweeting and so on aren't working now so I will have to come up with a special program to do it myself.

PAIN IN XXXXX!!!

45 weeks 9 hours ago
luzparet
@luzparet luz

why this code does not work?

$toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $ROW['tw_user_access_token'], $ROW['tw_access_token_secret']);

$followers = $toa->get('followers/ids', array());
$friends = $toa->get('friends/ids', array());

foreach ($followers->ids as $i => $id) {

if (empty($friends->ids) or !in_array($id, $friends->ids)) {
$ret = $toa->post('friendships/create', array('user_id' => $id));
if ($i++ > 20) break;
sleep(rand(3,30));
}

}
}

44 weeks 6 days ago
sinokt
@sinokt sino

How to increase the number of followers list using API 1.1 with oauth ? Currently i got only 20 followers. Is it possible to increase the limit ?

43 weeks 1 day ago
markfineza
@markfineza Mark Fineza

hi how can i get more followers instantly on my twitter?, no need to buy followers

41 weeks 6 days ago
PrashantAggar16
@PrashantAggar16 Prashant Aggarwal

hw to get following list in ios

17 weeks 2 days ago
dsharmalath
@dsharmalath DEEPAK SHARMA

how we can get our followers using javascript in asp.net

14 weeks 1 day ago