doufei16736 2015-12-06 21:24
浏览 52

PHP在发送下一个api请求之前等待

I have a website that pulls prices from an API. The problem is that if you send more than ~10 requests to this API in a short amount of time, your ip gets blocked temporarily (I'm not sure if this is just a problem on localhost or if it would also be a problem from the webserver, I assume the latter).

The request to the API returns a JSON object, which I then parse and store certain parts of it into my database. There are about 300 or so entries in the database, so ~300 requests I need to make to this API.

I will end up having a cron job that every x hours, all of the prices are updated from the API. The job calls a php script I have that does all of the request and db handling.

Is there a way to have the script send the requests over a longer period of time, rather than immediately? The problem I'm running into is that after ~20 or so requests the ip gets blocked, and the next 50 or so requests after that get no data returned.

I looked into sleep(), but read that it will just store the results in a buffer and wait, rather than wait after each request.

Here is the script that the cron job will be calling:

define('HTTP_NOT_FOUND', false);
define('HTTP_TIMEOUT', null);

function http_query($url, $timeout=5000) {
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT_MS, $timeout);

    $text = curl_exec($curl);

    if($text) {
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        switch($code){
            case 200:
                return $text;
            case 404:
                return -1;
            default:
                return -1;
            }
        }
    return HTTP_TIMEOUT;
}

function getPrices($ID) {

    $t = time();
    $url = url_to_API;
    $result = http_query($url, 5000);
    if ($result == -1) { return -1; }
    else {
        return json_decode($result)->price;
    }
}

connectToDB();

$result = mysql_query("SELECT * FROM prices") or die(mysql_error());

while ($row = mysql_fetch_array($result)) {
    $id = $row['id'];
    $updatedPrice = getItemPrices($id);
    .
    .
    echo $updatedPrice;
    . // here I am just trying to make sure I can get all ~300 prices without getting any php errors or the request failing (since the ip might be blocked)
    .
}
  • 写回答

1条回答 默认 最新

  • douyan1970 2015-12-06 21:53
    关注

    sleep() should not affect/buffer queries to the database. You can use ob_flush() if you need to print something immediately. Also make sure to set max execution time with set_time_limit() so your script don't timeout.

    set_time_limit(600);
    
    while ($row = mysql_fetch_array($result)) {
      $id = $row['id'];
      $updatedPrice = getItemPrices($id);
      .
      .
    
      echo $updatedPrice;
    
      //Sleep 1 seconds, use ob_flush if necessary
      sleep(1);
    
      //You can also use usleep(..) to delay the script in milliseconds
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch