PHP CURL與file_get_contents函數都可以獲取遠程服務器上的文件保存到本地,但在性能上面兩者完全不在同一個級別,下面我先來介紹PHP CURL或file_get_contents函數應用例子,然后再簡單的給各位介紹一下它們的一些小區別吧.
推薦方法 CURL獲取,代碼如下:
- <?php
- $c = curl_init();
- $url = 'www.49028c.com';
- curl_setopt($c, CURLOPT_URL, $url);
- curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
- $data = curl_exec($c);
- curl_close($c);
- $pos = strpos($data,'utf-8');
- if($pos===false){$data = iconv("gbk","utf-8",$data);}
- preg_match("/<title>(.*)<\/title>/i",$data, $title);
- echo $title[1];
- ?>
使用file_get_contents,代碼如下:
- <?php
- $content=file_get_contents("http://www.49028c.com/");
- $pos = strpos($content,'utf-8');
- if($pos===false){$content = iconv("gbk","utf-8",$content);}
- $postb=strpos($content,'<title>')+7;
- $poste=strpos($content,'</title>');
- $length=$poste-$postb;
- echo substr($content,$postb,$length);
- ?>
看看file_get_contents性能
1)fopen/file_get_contents 每次請求遠程URL中的數據都會重新做DNS查詢,并不對DNS信息進行緩存。但是CURL會自動對DNS信息進行緩存。對同一域名下的網頁或者圖片的請求只需要一次DNS 查詢。這大大減少了DNS查詢的次數。所以CURL的性能比fopen/file_get_contents 好很多。
2)fopen/file_get_contents在請求HTTP時,使用的是http_fopen_wrapper,不會keeplive。而curl卻可以。這樣在多次請求多個鏈接時,curl效率會好一些。(設置header頭應該可以)
3)fopen/file_get_contents函數會受到php.ini文件中allow_url_open選項配置的影響。如果該配置關閉了,則該函數也就失效了。而curl不受該配置的影響。
4)curl可以模擬多種請求,例如:POST數據,表單提交等,用戶可以按照自己的需求來定制請求。而fopen/file_get_contents只能使用get方式獲取數據。
5)fopen/file_get_contents 不能正確下載二進制文件
6)fopen/file_get_contents 不能正確處理ssl請求
7)curl 可以利用多線程
8)使用 file_get_contents 的時候如果網絡出現問題,很容易堆積一些進程在這里
9)如果是要打一個持續連接,多次請求多個頁面,那么file_get_contents就會出問題。取得的內容也可能會不對,所以做一些類似采集工作的時候,肯定就有問題了,對做采集抓取的用curl,如果還有同不相信下面我們再做個測試.
curl與file_get_contents性能對比PHP源代碼如下:
- <?php
- /**
- * 通過淘寶IP接口獲取IP地理位置
- * @param string $ip
- * @return: string
- **/
- function getCityCurl($ip)
- {
- $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
- $ch = curl_init();
- $timeout = 5;
- curl_setopt ($ch, CURLOPT_URL, $url);
- curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
- $file_contents = curl_exec($ch);
- curl_close($ch);
- $ipinfo=json_decode($file_contents);
- if($ipinfo->code=='1'){
- return false;
- }
- $city = $ipinfo->data->region.$ipinfo->data->city;
- return $city;
- }
- function getCity($ip)
- {
- $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
- $ipinfo=json_decode(file_get_contents($url));
- if($ipinfo->code=='1'){
- return false;
- }
- $city = $ipinfo->data->region.$ipinfo->data->city;
- return $city;
- }
- // for file_get_contents
- $startTime=explode(' ',microtime());
- $startTime=$startTime[0] + $startTime[1];
- for($i=1;$i<=10;$i++)
- {
- echo getCity("121.207.247.202")."</br>";
- }
- $endTime = explode(' ',microtime());
- $endTime = $endTime[0] + $endTime[1];
- $totalTime = $endTime - $startTime;
- echo 'file_get_contents:'.number_format($totalTime, 10, '.', "")." seconds</br>";
- //for curl
- $startTime2=explode(' ',microtime());
- $startTime2=$startTime2[0] + $startTime2[1];
- for($i=1;$i<=10;$i++)
- { //開源軟件:Vevb.com
- echo getCityCurl('121.207.247.202')."</br>";
- }
- $endTime2 = explode(' ',microtime());
- $endTime2=$endTime2[0] + $endTime2[1];
- $totalTime2 = $endTime2 - $startTime2;
- echo "curl:".number_format($totalTime2, 10, '.', "")." seconds";
- ?>
測試訪問:
file_get_contents速度:4.2404510975 seconds
curl速度:2.8205530643 seconds
curl比file_get_contents速度快了30%左右,最重要的是服務器負載更低.
新聞熱點
疑難解答