用于使用twitter API移动光标的PHP代码

时间:2012-11-12 00:21:14

标签: php api twitter cursor web-crawler

所以我已经有了一个脚本,它使用xml格式的API收集Twitter用户的前4999个粉丝id。我半理解游标过程是如何工作的,但我很困惑如何实现它循环直到它收集所有的追随者。我试图收集的用户将需要大约8个电话。关于如何实现游标循环的任何想法?

enter code here
<?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 id\n\r");
 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';
 ?>

1 个答案:

答案 0 :(得分:3)

让我举一个微软粉丝的追随者(346K粉丝)的例子。

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

它只获取5000个用户ID,即twitter API限制。因此,您需要从json输出中获取next_cursor字符串

next_cursor_str “:” 1418048755615786027"

所以,你的下一个电话将是

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

继续这样做,直到next_cursor为零。

当你一次又一次地做,只要继续存储所有的ID ..

相关问题