English 中文(简体)
Understanding Twitter API s "Cursor" parameter
原标题:

I don t really get how to use the Curesor parameter in Twitter s API, for an exmaple - here. Am I supposed to make a new API call for each 100 followers?

I d love it if someone could provide a PHP example for getting a full list of followers assuming I have more than 100...

Thanks in advance!

问题回答

You need to pass the cursor value back to the API to get the next "chunk" of followers. Then you take the cursor parameter from that chunk and pass it back to get the next chunk. It is like a "get the next page" mechanism.

Since this question was asked, Twitter API has changed in many ways.

Cursor is used to paginate API responses with many results. For example, a single API call to obtain followers will retrieve a maximum of 5000 ids.

If you want to obtain all the followers of a user you will have to make a new API call, but this time you have to indicate the "next_cursor" number that was in your first response.

If it s useful, the following python code will retrieve followers from a given user.

It will retrieve a maximum of pages indicated by a constant.

Be careful not to be banned (i.e.: don t make more than 150 api calls/hour in anonymous calls)

import requests
import json
import sys


screen_name = sys.argv[1]
max_pages = 5
next_cursor = -1

followers_ids = []

for i in range(0,max_pages):
    url =  https://api.twitter.com/1/followers/ids.json?screen_name=%s&cursor=%s  % (screen_name, next_cursor)
    content = requests.get(url).content
    data = json.loads(content)
    next_cursor = data[ next_cursor ]

    followers_ids.extend(data[ ids ])

print "%s have %s followers!" % (screen_name, str(len(followers_ids)))

Despite you asked it some time ago, I hope this will be the more accurated answer.

  1. Twitter does not offer info about what next_cursor means. Only how cursor works.
  2. It is only an easy way for Twitter to manage pagination.
  3. «The function is intended to be used linearly, one cursor set at a time per user.» Source

«It s potentially less efficient for you but much more efficient for us.» Twitter Staff

BUT... Someone ask before in a broken link «are cursors persistents? seem that the answer is "yes"»

This means you can save your last cursor before 0 and continue with it next time.

Check out http://code.google.com/p/twitter-boot/source/browse/trunk/twitter-bot.php

foreach ($this->twitter->getFollowers(,0 ) as $follower)//the 0 is the page
    {
      if ($this->twitter->existsFriendship($this->user, $follower[ screen_name ])) //If You  Follow this user
          continue;    //no need to follow now;
      try
      {
        $this->twitter->createFriendship($follower[ screen_name ], true); // If you dont Follow Followit now
        $this->logger->debug( Following new follower:  .$follower[ screen_name ]);
      }
      catch (Exception $e)
      {
        $this->logger->debug("Skipping:".$follower[ screen_name ]." ".$e->getMessage());
      }

    }

  }




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签