我构建了一个 php cli 脚本,它使用 cURL 将推文从 twitter 流 api 输出到命令行。当我启动脚本时,它开始输出推文,但在每 50 条推文时,我都会收到以下错误:“很容易,Turbo。最近请求太多。增强你的冷静。”,没有标题,没有其他输出,只有一个字符串。
我假设我建立了太多的连接,但我只初始化了一次 cURL。这是我的代码:
//initialize cURL connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://stream.twitter.com/1.1/statuses/filter.json? follow=34572451,27260086');
curl_setopt($ch, CURLOPT_USERPWD, 'USERNAME:PASSWORD');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'saveTweet');
curl_setopt($ch, CURLOPT_MAXCONNECTS, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Cache-Control: max-age=0', 'Connection: keep-alive', 'Keep-Alive: 3600'));
//then loop the execution to retrieve tweets
while(1){
echo $i++.'->'; //echo number, so we know it stops at 50
try {
//calls saveTweet() as callback, which only purpose is to output the tweet
curl_exec($ch);
}
catch(Exception $e) {
//echo error described above
echo $e->getMessage();
break;
}
}
如何防止 cURL 发出太多请求(如果这样做,我真的不知道确切的问题)?或者如何防止出现上述错误?