如何在固定的时间范围内暂停此循环?

I do have a array which will read text file and do api call and then insert these result to mySQL. but when it run in bulk the response from API server is slow and due to this many of the results are coming blank. what I am looking is is there a way to pause this loop for each call say 5 seconds to get result from api server so it wont get blank results.

this code is below

//connect to your database 
mysql_connect("localhost", "root", "password"); 
mysql_select_db("somedb"); 

//read your text file into an array, one entry per line 
$lines = file('filename.txt'); 

//loop through each website URL  
foreach ($lines as $website_url) { 

    //make the request to the compete API 
    $response = file_get_contents("http://apps.compete.com/sites/" . $website_url . "/trended/rank/?apikey=0sdf456sdf12sdf1"); 

    //decode the request 
    $response = json_decode($request); 

    //get the rank from the response 
    $rank = $response['something'];  

    //insert the website URL and its rank 
    mysql_query("INSERT INTO website_ranks (website_url, rank) VALUES ('" . mysql_real_escape_string($website_Url) . "', " . $rank . ")"); 

} 

Use the sleep command

 sleep (5);

Wouldn't it make more sense to verify the server responded rather than sleeping for an arbitrary amount of time?

One alternate approach I'd recommend using instead of looping with a single INSERT statement is using a BULK INSERT syntax with your RDBMS provider. This will speed up the process considerably.

That syntax usually looks like this:

INSERT INTO tablename(col1, col2) VALUES (...)