简体   繁体   中英

Fetch all Twitter followers using PHP API with Cursors

How to use cursors for the Twitter API?

I wrote a PHP script in which I want to fetch all Twitter followers. But where to place cursors? When I tested it with 1000 followers it worked but when the number of followers is more than 6000 or 10000 it fetches 5000 ids at max only.

I want to fetch all followers, where to put cursors? My code is:

    <?php
ini_set('max_execution_time', 600);
ini_set('memory_limit', '1024M');
require_once('twitteroauth.php');
$consumerKey = 'key1';
$consumerKeySecret = 'key2';
$accessToken = 'key3';
$accessTokenSecret = 'key4';
$cursor = -1; // first page

    while( $cursor != 0 ){
        $connection = new TwitterOAuth($consumerKey, $consumerKeySecret, $accessToken, $accessTokenSecret);
        $profiles = array();
        $sc_name = 'LarryWentz';
        $cursor = "&cursor=" + $cursor; 
        $ids = $connection->get("https://api.twitter.com/1.1/friends/ids.json?screen_name=$sc_name".$cursor);
        $cursor = $ids->next_cursor;
        if(!is_array($ids->ids)) break;
        $ids_arrays = array_chunk($ids->ids, 100);
        $i=1;
        foreach($ids_arrays as $implode) {
            $user_ids=implode(',', $implode);
            $results = $connection->get("https://api.twitter.com/1.1/users/lookup.json?user_id=$user_ids");
            foreach($results as $profile) {
                $profiles[$profile->name] = $profile;
            }
        }
    }
    foreach($profiles as $profile) 
    {
    echo $i. "-" .$profile->name . "<br />";
    $i++;
    }
?>

Assuming you use this library https://github.com/abraham/twitteroauth :

$profiles = array();
$sc_name = 'LarryWentz';
$cursor = -1;
while ($cursor != 0) {
    $ids = $connection->get("friends/ids", array("screen_name" => $sc_name, "cursor" => $cursor));
    $cursor = $ids->next_cursor;
    $ids_arrays = array_chunk($ids->ids, 100);
    foreach($ids_arrays as $implode) {
        $user_ids=implode(',', $implode);
        $results = $connection->get("users/lookup.json", array("user_id" => $user_ids));
        foreach($results as $profile) {
          $profiles[$profile->name] = $profile;
        }
    }
}

Results are given in groups of 5,000 user IDs and multiple “pages” of results can be navigated through using the next_cursor value in subsequent requests. See Using cursors to navigate collections for more information. - from https://dev.twitter.com/rest/reference/get/friends/ids

So thats why you are getting max 5000 results and you have to use the cursor. You have to put your code in a loop like this: Here is what you need to do with the cursor:

...

<?php
ini_set('max_execution_time', 600);
ini_set('memory_limit', '1024M');
require_once('twitteroauth.php');
$consumerKey = 'k1';
$consumerKeySecret = 'k2';
$accessToken = 'k3';
$accessTokenSecret = 'k4';
$cursor = -1; // first page
$profiles = array();
    while( $cursor != 0 ){
        $connection = new TwitterOAuth($consumerKey, $consumerKeySecret, $accessToken, $accessTokenSecret);

        $sc_name = 'savanpaun';
        $cursor = "&cursor=" + $cursor; 
        $ids = $connection->get("https://api.twitter.com/1.1/friends/ids.json?screen_name=$sc_name".$cursor);
        $cursor = $ids->next_cursor;
        if(!is_array($ids->ids)) break;
        $ids_arrays = array_chunk($ids->ids, 100);
        $i=1;
        foreach($ids_arrays as $implode) {
            $user_ids=implode(',', $implode);
            $results = $connection->get("https://api.twitter.com/1.1/users/lookup.json?user_id=$user_ids");
            foreach($results as $profile) {
                $profiles[$profile->name] = $profile;
            }
        }
    }
    foreach($profiles as $profile) 
    {
    echo $i. "-" .$profile->name . "<br />";
    $i++;
    }
?>

This should do it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM