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)
    .
}

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
}