PHP CURL或file_get_contents獲取網(wǎng)頁(yè)標(biāo)題的代碼及兩者效率的穩(wěn)定性問(wèn)題
PHP CURL與file_get_contents函數(shù)都可以獲取遠(yuǎn)程服務(wù)器上的文件保存到本地,但在性能上面兩者完全不在同一個(gè)級(jí)別,下面我先來(lái)介紹PHP CURL或file_get_contents函數(shù)應(yīng)用例子,然后再簡(jiǎn)單的給各位介紹一下它們的一些小區(qū)別吧。
推薦方法 CURL獲取
<?php $c = curl_init(); $url = 'chabaoo.cn'; 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://chabaoo.cn/"); $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 每次請(qǐng)求遠(yuǎn)程URL中的數(shù)據(jù)都會(huì)重新做DNS查詢(xún),并不對(duì)DNS信息進(jìn)行緩存。但是CURL會(huì)自動(dòng)對(duì)DNS信息進(jìn)行緩存。對(duì)同一域名下的網(wǎng)頁(yè)或者圖片的請(qǐng)求只需要一次DNS 查詢(xún)。這大大減少了DNS查詢(xún)的次數(shù)。所以CURL的性能比f(wàn)open/file_get_contents 好很多。
2)fopen/file_get_contents在請(qǐng)求HTTP時(shí),使用的是http_fopen_wrapper,不會(huì)keeplive。而curl卻可以。這樣在多次請(qǐng)求多個(gè)鏈接時(shí),curl效率會(huì)好一些。(設(shè)置header頭應(yīng)該可以)
3)fopen/file_get_contents函數(shù)會(huì)受到php.ini文件中allow_url_open選項(xiàng)配置的影響。如果該配置關(guān)閉了,則該函數(shù)也就失效了。而curl不受該配置的影響。
4)curl可以模擬多種請(qǐng)求,例如:POST數(shù)據(jù),表單提交等,用戶(hù)可以按照自己的需求來(lái)定制請(qǐng)求。而fopen/file_get_contents只能使用get方式獲取數(shù)據(jù)。
5)fopen/file_get_contents 不能正確下載二進(jìn)制文件
6)fopen/file_get_contents 不能正確處理ssl請(qǐng)求
7)curl 可以利用多線(xiàn)程
8)使用 file_get_contents 的時(shí)候如果 網(wǎng)絡(luò)出現(xiàn)問(wèn)題, 很容易堆積一些進(jìn)程在這里
9)如果是要打一個(gè)持續(xù)連接,多次請(qǐng)求多個(gè)頁(yè)面。那么file_get_contents就會(huì)出問(wèn)題。取得的內(nèi)容也可能會(huì)不對(duì)。所以做一些類(lèi)似采集工作的時(shí)候,肯定就有問(wèn)題了。對(duì)做采集抓取的用curl,如果還有同不相信下面我們?cè)僮鰝€(gè)測(cè)試
curl與file_get_contents性能對(duì)比PHP源代碼如下:
1829.php
<?php /** * 通過(guò)淘寶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++) { 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"; ?>
測(cè)試訪(fǎng)問(wèn)
file_get_contents速度:4.2404510975 seconds
curl速度:2.8205530643 seconds
curl比f(wàn)ile_get_contents速度快了30%左右,最重要的是服務(wù)器負(fù)載更低.
ps:php函數(shù)file_get_contents與curl效率及穩(wěn)定性問(wèn)題
習(xí)慣了使用方便快捷的file_get_contents函數(shù)抓取別家網(wǎng)站內(nèi)容,但是總是會(huì)遇到獲取失敗的問(wèn)題,盡管按照手冊(cè)中的例子設(shè)置了超時(shí),可多數(shù)時(shí)候不好使:
$config['context'] = stream_context_create(array('http' => array('method' => "GET",'timeout' => 5)));
'timeout' => 5//這個(gè)超時(shí)時(shí)間不穩(wěn)定,經(jīng)常不好使。這時(shí)候,看一下服務(wù)器的連接池,會(huì)發(fā)現(xiàn)一堆類(lèi)似下面的錯(cuò)誤,讓你頭疼萬(wàn)分:
file_get_contents(http://***): failed to open stream…
不得已,安裝了curl庫(kù),寫(xiě)了一個(gè)函數(shù)替換:
function curl_get_contents($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //設(shè)置訪(fǎng)問(wèn)的url地址 //curl_setopt($ch,CURLOPT_HEADER,1); //是否顯示頭部信息 curl_setopt($ch, CURLOPT_TIMEOUT, 5); //設(shè)置超時(shí) curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_); //用戶(hù)訪(fǎng)問(wèn)代理 User-Agent curl_setopt($ch, CURLOPT_REFERER,_REFERER_); //設(shè)置 referer curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); //跟蹤301 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回結(jié)果 $r = curl_exec($ch); curl_close($ch); return $r; }
如此,除了真正的網(wǎng)絡(luò)問(wèn)題外,沒(méi)再出現(xiàn)任何問(wèn)題。
這是別人做過(guò)的關(guān)于curl和file_get_contents的測(cè)試:
file_get_contents抓取google.com需用秒數(shù):
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
curl使用的時(shí)間:
0.68719101
0.64675593
0.64326
0.81983113
0.63956594
差距很大吧?呵呵,從我使用的經(jīng)驗(yàn)來(lái)說(shuō),這兩個(gè)工具不只是速度有差異,穩(wěn)定性也相差很大。建議對(duì)網(wǎng)絡(luò)數(shù)據(jù)抓取穩(wěn)定性要求比較高的朋友使用上面的 curl_file_get_contents函數(shù),不但穩(wěn)定速度快,還能假冒瀏覽器欺騙目標(biāo)地址哦!
- php基于curl重寫(xiě)file_get_contents函數(shù)實(shí)例
- php中file_get_contents與curl性能比較分析
- php采用file_get_contents代替使用curl實(shí)例
- 深入file_get_contents與curl函數(shù)的詳解
- 探討file_get_contents與curl效率及穩(wěn)定性的分析
- 比f(wàn)ile_get_contents穩(wěn)定的curl_get_contents分享
- php中使用Curl、socket、file_get_contents三種方法POST提交數(shù)據(jù)
- PHP curl 或 file_get_contents 獲取需要授權(quán)頁(yè)面的方法
相關(guān)文章
Laravel Memcached緩存驅(qū)動(dòng)的配置與應(yīng)用方法分析
這篇文章主要介紹了Laravel Memcached緩存驅(qū)動(dòng)的配置與應(yīng)用方法,結(jié)合實(shí)例形式分析了在Laravel框架配置Memcached緩存及相關(guān)使用方法,需要的朋友可以參考下2016-10-10thinkphp 框架數(shù)據(jù)庫(kù)切換實(shí)現(xiàn)方法分析
這篇文章主要介紹了thinkphp 框架數(shù)據(jù)庫(kù)切換實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkphp 框架數(shù)據(jù)庫(kù)切換實(shí)現(xiàn)方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05Laravel 5框架學(xué)習(xí)之模型、控制器、視圖基礎(chǔ)流程
本文給大家介紹的是Laravel5框架中模型、控制器、視圖的基礎(chǔ)流程,其實(shí)MVC體系結(jié)構(gòu)模式將一個(gè)交互式系統(tǒng)分為三個(gè)組件。模型包含核心功能和數(shù)據(jù)。視圖向用戶(hù)顯示信息??刂破魈幚碛脩?hù)輸入。視圖和控制器共同構(gòu)成了用戶(hù)接口。2015-04-04php實(shí)現(xiàn)無(wú)限級(jí)分類(lèi)
這篇文章主要介紹了php實(shí)現(xiàn)無(wú)限級(jí)分類(lèi),方法非常的簡(jiǎn)單,代碼也很難簡(jiǎn)潔,需要的朋友可以參考下2014-12-12Yii框架獲取當(dāng)前controlle和action對(duì)應(yīng)id的方法
這篇文章主要介紹了Yii框架獲取當(dāng)前controlle和action對(duì)應(yīng)id的方法,可實(shí)現(xiàn)獲取當(dāng)前controlle或action對(duì)應(yīng)id的功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12在Laravel中實(shí)現(xiàn)使用AJAX動(dòng)態(tài)刷新部分頁(yè)面
今天小編就為大家分享一篇在Laravel中實(shí)現(xiàn)使用AJAX動(dòng)態(tài)刷新部分頁(yè)面,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10golang實(shí)現(xiàn)php里的serialize()和unserialize()序列和反序列方法詳解
這篇文章主要介紹了golang實(shí)現(xiàn)php里的serialize()和unserialize()序列和反序列方法詳解,需要的朋友可以參考下2018-10-10PHP使用mysqli同時(shí)執(zhí)行多條sql查詢(xún)語(yǔ)句的實(shí)例
今天小編就為大家分享一篇關(guān)于PHP使用mysqli同時(shí)執(zhí)行多條sql查詢(xún)語(yǔ)句的實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03php數(shù)據(jù)序列化測(cè)試實(shí)例詳解
這篇文章主要介紹了php數(shù)據(jù)序列化測(cè)試實(shí)例詳解的相關(guān)資料,主要介紹msgpack、json、serialize對(duì)比,需要的朋友可以參考下2017-08-08