Skip to content
Advertisement

PHP code for moving the cursor using the twitter API

So I already have a script that collects the first 4999 followers ids of a twitter user using the API in xml format. I semi understand how the cursor process works but I am confused how to implement it to loop until it gathers all the followers. The user I am attempting to gather will take about 8 calls. Any ideas on how to implement the cursor loop?

<?php
 $xmldata = 'http://api.twitter.com/1/followers/ids/microsoft.xml';
 $open = fopen($xmldata, 'r');
 $content = stream_get_contents($open);
 fclose($open);
 $xml = simplexml_load_file($xmldata);
 $cursor = $xml->next_cursor;
 $file = fopen ('output1.csv', 'w+');
fwrite($file, "User idnr");
 while($cursor =! 0)
 {
 foreach ($xml->ids->id as $id) 
 {
    fwrite($file, $id . ", ");
fwrite($file, "n");


 }
 $xmldata = 'http://api.twitter.com/1/followers/ids.xml?cursor='. $cursor
.'&screeb_name=microsoft';
 ?>

Advertisement

Answer

Let me take an example of Microsoft’s followers (346K followers) as of now.

https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=microsoft

It fetches only 5000 user IDs, that the twitter API limit. So, you need to take the next_cursor string from the json output

next_cursor_str”:”1418048755615786027″

So, your next call would be

https://api.twitter.com/1/followers/ids.json?cursor=1418048755615786027&screen_name=microsoft

Keep doing this until the next_cursor is ZERO.

As you keep doing again and again, just keep storing all the ids..

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement