2012-02-29 62 views
0

有沒有人知道什麼是最好的(或非常好的)方法是從api加載外部文件(大約10-20),並考慮性能。每個會話都有不同的內容。目前我嘗試「file_get_contents」,但遇到嚴重的性能問題。我對Curl並不是很熟悉,但它似乎擊敗了古老的PHP方式的性能。任何想法/例子?如何使用PHP加載多個外部文件 - 速度很快?

+0

「好老PHP辦法」? – 2012-02-29 02:45:15

+0

file_get_contents – 2012-02-29 02:48:19

+0

file_get_contents僅在PHP 4.3.0中引入 - 它不是那麼陳舊 – 2012-02-29 02:50:01

回答

1

你也可以使用捲曲多一次搶多個文件,有一個教程在這裏:

http://www.phpied.com/simultaneuos-http-requests-in-php-with-curl/

<?php 
//Copy & pasted from the above link 
function multiRequest($data, $options = array()) { 
    // array of curl handles 
    $curly = array(); 
    // data to be returned 
    $result = array(); 
    // multi handle 
    $mh = curl_multi_init(); 
    // loop through $data and create curl handles 
    // then add them to the multi-handle 
    foreach ($data as $id => $d) { 
    $curly[$id] = curl_init(); 
    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; 
    curl_setopt($curly[$id], CURLOPT_URL,   $url); 
    curl_setopt($curly[$id], CURLOPT_HEADER,   0); 
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); 
    // post? 
    if (is_array($d)) { 
     if (!empty($d['post'])) { 
     curl_setopt($curly[$id], CURLOPT_POST,  1); 
     curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); 
     } 
    } 
    // extra options? 
    if (!empty($options)) { 
     curl_setopt_array($curly[$id], $options); 
    } 
    curl_multi_add_handle($mh, $curly[$id]); 
    } 
    // execute the handles 
    $running = null; 
    do { 
    curl_multi_exec($mh, $running); 
    } while($running > 0); 
    // get content and remove handles 
    foreach($curly as $id => $c) { 
    $result[$id] = curl_multi_getcontent($c); 
    curl_multi_remove_handle($mh, $c); 
    } 
    // all done 
    curl_multi_close($mh); 
    return $result; 
} 
?> 

<?php 
$data = array(
    'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Pearl+Jam&output=json', 
    'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Pearl+Jam&output=json', 
    'http://search.yahooapis.com/AudioSearchService/V1/artistSearch?appid=YahooDemo&artist=Pearl+Jam&output=json' 
); 
$r = multiRequest($data); 

echo '<pre>'; 
print_r($r); 
/* 
Array 
(
    [0] => {"ResultSet":{"totalResultsAvailable":"633","totalResultsReturned":... 
    [1] => {"ResultSet":{"totalResultsAvailable":"105342","totalResultsReturned":... 
    [2] => {"ResultSet":{"totalResultsAvailable":10,"totalResultsReturned":... 
) 
*/ 
?> 

或捲曲的PHP文件http://www.php.net/manual/en/function.curl-multi-init.php

+0

正是我以後的樣子。謝謝@勞倫斯! – 2012-02-29 03:11:52

+0

好,很樂意幫忙 – 2012-02-29 03:13:47

+0

僅供參考 - With Curl我將頁面加載時間從18秒減少到3秒!再次感謝。 – 2012-03-01 03:07:29

0

file_get_contents可能是最快的方法,因爲它只需要PHP中的一個函數調用,而其他方法如fopen/fread/fclose需要多次調用,就像cUrl一樣。

但是fopen的優點是不需要RAM等於或大於文件大小,因爲您可以逐個處理文件。

總的來說,file_get_contents是一個很好的通用功能,但根據情況影響,您可能需要一個不同的選項。

+0

嗨科爾尼克,謝謝你。你知道是否有任何方法可以讓你提到的技術有更好的表現? – 2012-02-29 02:54:18

+0

不是。只要使用正確的工具來完成這項工作,那就是最重要的。 – 2012-02-29 03:05:34

相關問題