Search code examples
phpapitwittertwitter-oauth

Twitter API Cursor(pagination) PHP


I list people who do not follow me that I follow. I want to implement this code in my own code.

I use abraham's Twitter API library.

I can take max 200 data(friends/list, followers/list). I need more.

I can not. Can you help me? thank you.

My Code:

<?php
  require_once('twitteroauth/OAuth.php');
  require_once('twitteroauth/twitteroauth.php');
  $baglanti = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, USER_TOKEN, USER_TOKEN_SECRET);
  $takipEttiklerim = $baglanti->get('friends/list', ["count" => 200]);
  $takipEttiklerim = array($takipEttiklerim);
  $takipEttiklerim = json_decode(json_encode($takipEttiklerim), true);

  $takipEdenler = $baglanti->get('followers/list', ["count" => 200]);
  $takipEdenler = array($takipEdenler);
  $takipEdenler = json_decode(json_encode($takipEdenler), true);

  $array = array();

    for ($i = 0; $i < count($takipEdenler[0]["users"]); $i++) {
      array_push($array, $takipEdenler[0]["users"][$i]["screen_name"]);
    }
    for ($i = 0; $i < count($takipEttiklerim[0]["users"]); $i++) {
      if (!in_array($takipEttiklerim[0]["users"][$i]["screen_name"], $array)) {
        echo $takipEttiklerim[0]["users"][$i]["name"];
      }
    }
?>

Output:

Profile_1
Profile_2
...
Profile_200 (max)

Solution

  • Oh! This is a poorly created and old question by me. But I am answering it so that it will be useful for beginners who have such problems.

    As stated in the twitter doc, each query returns a new cursor. If you initialize the previous cursor you have before submitting a new query, it will return the next page.

    Example response:

    {
    "users": [
          {user-object},
          {user-object},
          {user-object}
      ],
      "previous_cursor": 0,
      "previous_cursor_str": "0",
      "next_cursor": 1333504313713126852,
      "next_cursor_str": "1333504313713126852"
    }