0
votes

From REST API of twitter I user get/followers function. I pasted a code snippet below.

My problem is, modt of the time I get followers' ids successfully. But when a user has more than 5000-6000 followers then my results comes wrong.

When I check from user's profile page, I see that user has 5500 followers, but when I run following code, most of the time 5500 ids come, but sometimes 29994 followers come inside $ids variable. Now I'm logging the results having more then 29k followers. I saw some of the requests returned with 29994 followers, but I couldn't find the answer.

Do I miss something in get ids - cursor approach? Thank you

Edit: After some debugging I logged "$cursor_archieve" parameter and found out this:
* Sometimes next_cursor and previous_cursor comes same:

Array
(
    [0] => -1
    [1] => 1400573121087317741
    [2] => 1400573121087317741
    [3] => 1400573121087317741
    [4] => 1400573121087317741
    [5] => 1400573121087317741
    [6] => 1400573121087317741
)

So in this situation, although user has 7100 followers I get only 5000 followers

  • Sometimes cursors come sequentially same:

    Array
    (
    [0] => -1
    [1] => 1404335879106773348
    [2] => 1404341060469987526
    [3] => 1404338682006540390
    [4] => 1404341060469987526
    [5] => 1404335879106773348
    [6] => 1404338682006540390
    )

My code is like this:

public function getIds($user = "someuser"){
    $tmhOAuth = new tmhOAuth(array( 'consumer_key'    => YOUR_CONSUMER_KEY, 
              'consumer_secret' => YOUR_CONSUMER_SECRET,
              'user_token'      => $atoken1, 'user_secret'     => $atoken2, ));

    $cursor = '-1';
    $ids = array();
    $cursor_archieve = array();
    while(true):
        $code=$tmhOAuth->request('GET', $tmhOAuth->url('1/followers/ids'),
                array('screen_name' => $user, 'cursor' => $cursor));

        if ($code == 200) {
            $responseData = json_decode($tmhOAuth->response['response'],true);
            $ids = array_merge($ids, $responseData['ids']);
            $cursor = $responseData['next_cursor_str'];
                    $cursor_archieve[] = $cursor;
        } else {
            return 0;
        }
        if ( $cursor == '0' || count($ids) >= 29000 ) {
            break;
        }
    endwhile;

    return $ids;
}

edit2: Should I make "array_unique" to delete duplicate ids, or doesn't use next cursor if previous_cursor=next cursor or any other option? In every case user has 5500-6500 followers. So If I take only one cursor, I only can get first 5000 followers.

1

1 Answers

0
votes

The reason was a programatic error in my codes. I fixed it after a week's debug session