我构建了一个php cli脚本,使用cURL从twitter流api输出推文到命令行。当我开始编写脚本时,它开始输出推文,但在每50条推文中我都会收到以下错误:“那里很容易,Turbo。最近请求太多。增强你的平静。”,没有标题,没有其他输出,只是一个字符串。< / p>
我假设我连接的次数过多,但我只启动了一次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发出太多请求(如果这样做,我真的不知道确切的问题)?或者我如何防止出现上述错误?