0
我有陣列,例如獲取數據:最好的辦法從其他服務
$links = array(
'http://aaa.com/data.txt',
'http://aaea.com/data.txt',
'http://aada.com/data.txt',
'http://agaa.com/data.txt',
'http://ahaa.com/data.txt',
'http://awha.com/data.txt',
'http://aaeha.com/data.txt',
//etc x100
);
而在PHP我做:
foreach ($links as $link) {
$data = file_get_contents($link);
//save data in database
}
它工作正常,但非常緩慢。用PHP做這個更好的方法是什麼?我想讓數據異步。
我的另一種方式 - 從PHP腳本的jQuery和Ajax查詢,但也許存在更好的方式?
PHP是單線程的(雖然有一些多線程庫),所以你將無法在腳本中非常容易地異步獲取。 其他選項包括緩存這些數據文件,並且只在它們過期時刷新它們(您可以從URL中獲取標題以嘗試查找日期時間)。 – fbas
嘗試http://php.net/manual/en/function.curl-multi-exec.php – LiTe
您可能會發現這有助於:http://stackoverflow.com/questions/15559157/understanding-php-curl-multi- exec –