《PHP實(shí)例:PHP CURL或file_get_contents獲取網(wǎng)頁標(biāo)題的代碼及兩者效率的穩(wěn)定性問題》要點(diǎn):
本文介紹了PHP實(shí)例:PHP CURL或file_get_contents獲取網(wǎng)頁標(biāo)題的代碼及兩者效率的穩(wěn)定性問題,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP學(xué)習(xí)PHP CURL與file_get_contents函數(shù)都可以獲取遠(yuǎn)程服務(wù)器上的文件保留到本地,但在性能上面兩者完全不在同一個(gè)級別,下面我先來介紹PHP CURL或file_get_contents函數(shù)應(yīng)用例子,然后再簡單的給各位介紹一下它們的一些小區(qū)別吧.
PHP學(xué)習(xí)推薦辦法 CURL獲取
PHP學(xué)習(xí)
<?php
$c = curl_init();
$url = 'www.jb51.net';
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];
?>
PHP學(xué)習(xí)使用file_get_contents
PHP學(xué)習(xí)
<?php
$content=file_get_contents("/");
$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);
?>
PHP學(xué)習(xí)看看file_get_contents性能
PHP學(xué)習(xí)1)fopen/file_get_contents 每次哀求遠(yuǎn)程URL中的數(shù)據(jù)都會重新做DNS查詢,并不對DNS信息進(jìn)行緩存.但是CURL會自動(dòng)對DNS信息進(jìn)行緩存.對同一域名下的網(wǎng)頁或者圖片的哀求只需要一次DNS 查詢.這大大減少了DNS查詢的次數(shù).所以CURL的性能比fopen/file_get_contents 好很多.
2)fopen/file_get_contents在哀求HTTP時(shí),使用的是http_fopen_wrapper,不會keeplive.而curl卻可以.這樣在多次哀求多個(gè)鏈接時(shí),curl效率會好一些.(設(shè)置header頭應(yīng)該可以)
3)fopen/file_get_contents函數(shù)會受到php.ini文件中allow_url_open選項(xiàng)配置的影響.如果該配置關(guān)閉了,則該函數(shù)也就失效了.而curl不受該配置的影響.
4)curl可以模擬多種哀求,例如:POST數(shù)據(jù),表單提交等,用戶可以按照自己的需求來定制哀求.而fopen/file_get_contents只能使用get方式獲取數(shù)據(jù).
5)fopen/file_get_contents 不能正確下載二進(jìn)制文件
6)fopen/file_get_contents 不能正確處理ssl哀求
7)curl 可以利用多線程
8)使用 file_get_contents 的時(shí)候如果 網(wǎng)絡(luò)出現(xiàn)問題, 很容易堆積一些進(jìn)程在這里
9)如果是要打一個(gè)持續(xù)連接,多次哀求多個(gè)頁面.那么file_get_contents就會出問題.取得的內(nèi)容也可能會不對.所以做一些類似采集工作的時(shí)候,肯定就有問題了.對做采集抓取的用curl,如果還有同不相信下面我們再做個(gè)測試
PHP學(xué)習(xí)curl與file_get_contents性能對比PHP源代碼如下:
PHP學(xué)習(xí)1829.php
PHP學(xué)習(xí)
<?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++)
{
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";
?>
PHP學(xué)習(xí)測試拜訪
PHP學(xué)習(xí)file_get_contents速度:4.2404510975 seconds
curl速度:2.8205530643 seconds
curl比file_get_contents速度快了30%左右,最重要的是服務(wù)器負(fù)載更低.
PHP學(xué)習(xí)ps:php函數(shù)file_get_contents與curl效率及穩(wěn)定性問題
PHP學(xué)習(xí)習(xí)慣了使用方便快捷的file_get_contents函數(shù)抓取別家網(wǎng)站內(nèi)容,但是總是會遇到獲取失敗的問題,盡管依照手冊中的例子設(shè)置了超時(shí),可多數(shù)時(shí)候不好使:
PHP學(xué)習(xí)$config['context'] = stream_context_create(array('http' => array('method' => "GET",'timeout' => 5)));?
'timeout' => 5//這個(gè)超時(shí)時(shí)間不穩(wěn)定,經(jīng)常不好使.這時(shí)候,看一下服務(wù)器的連接池,會發(fā)現(xiàn)一堆類似下面的錯(cuò)誤,讓你頭疼萬分:
PHP學(xué)習(xí)file_get_contents(http://***): failed to open stream…?
PHP學(xué)習(xí)不得已,安裝了curl庫,寫了一個(gè)函數(shù)替換:
PHP學(xué)習(xí)
function curl_get_contents($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //設(shè)置拜訪的url地址
//curl_setopt($ch,CURLOPT_HEADER,1); //是否顯示頭部信息
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //設(shè)置超時(shí)
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_); //用戶拜訪代理 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;
}
PHP學(xué)習(xí)如此,除了真正的網(wǎng)絡(luò)問題外,沒再出現(xiàn)任何問題.
PHP學(xué)習(xí)這是別人做過的關(guān)于curl和file_get_contents的測試:
PHP學(xué)習(xí)file_get_contents抓取google.com需用秒數(shù):
PHP學(xué)習(xí)2.31319094??
2.30374217??
2.21512604??
3.30553889??
2.30124092?
PHP學(xué)習(xí)curl使用的時(shí)間:
PHP學(xué)習(xí)0.68719101??
0.64675593??
0.64326??
0.81983113??
0.63956594?
PHP學(xué)習(xí)差距很大吧?呵呵,從我使用的經(jīng)驗(yàn)來說,這兩個(gè)工具不只是速度有差異,穩(wěn)定性也相差很大.建議對網(wǎng)絡(luò)數(shù)據(jù)抓取穩(wěn)定性要求比擬高的朋友使用上面的 curl_file_get_contents函數(shù),不但穩(wěn)定速度快,還能假冒瀏覽器欺騙目標(biāo)地址哦!
歡迎參與《PHP實(shí)例:PHP CURL或file_get_contents獲取網(wǎng)頁標(biāo)題的代碼及兩者效率的穩(wěn)定性問題》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/8341.html