I built an php cli script that ouputs tweets from twitter streaming api to the commandline with cURL. When I start the script it begins outputting tweets, but at every 50th tweet I get the following error: "Easy there, Turbo. Too many requests recently. Enhance your calm.", no headers, no other ouput, just a string.
I assume I make too many connections, but I only init the cURL once. Here's my code:
//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;
}
}
How do I prevent cURL from making too many requests (if it does that, I don't really know the exact problem)? Or how do I prevent getting the above error?