I'm trying to throttle my curl requests to keep under the GET request limits set by the server's RESTful API. Essentially, I'm looping through an array of IDs and looking to extract a base_bid to an array. I'm trying to add a delay between each request and have tried using PHP's usleep() function to do so but this only seems to add a delay for the first iteration and not onwards.
I'm using the code below:
$test = array();
for ($i=0; $i < count($campaigns); $i++) {
$ch = curl_init('https://api.appnexus.com/campaign?id='.$campaigns[$i]['id'].'');
$options = array(
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization:'.$token[1].''
));
curl_setopt_array($ch, $options);
$base = curl_exec($ch);
$info = curl_getinfo($ch);
//Attempting to throttle curl here
usleep(5000);
curl_close($ch);
if ($base === false || $info['http_code'] != 200) {
$output = 'Status code: '.$info['httpcode'].'';
} else {
preg_match('/"base_bid":([0-9\.]+)/', $base ,$bid);
$test[] = array(
'id' => $campaigns[$i]['id'],
'base_bid' => $bid[1]
);
}
};
Is this how usleep() is meant to be implemented in a loop for curl requests? Any comments would be highly valued!
Thanks,
Sam