我有一個數組,它將讀取文本文件並執行api調用,然後將這些結果插入到mySQL中。但是當它批量運行時,來自API服務器的響應速度很慢,並且由於許多結果都是空白的。我在看的是有沒有一種方法來暫停每個呼叫的循環說5秒從api服務器獲得結果,所以它不會得到空白的結果。如何暫停此循環的固定時間?
這個代碼如下
//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 . ")");
}
`睡眠(5)`不會做呢? – ircmaxell 2010-12-22 20:39:04