duanchun6148 2016-04-21 09:24
浏览 25

在PHP问题中限制卷曲

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行