- 相干保举
php中file-get-contents与curl机能比拟阐发
在php中若是不细心的去阐发机能会发明file_get_contents与curl两个同良多配合点的,他们都能够收罗文件翻开文件,可是若是细心一对照会发明良多差别点,以下是小编为大师搜刮清算的php中file_get_contents与curl机能比拟阐发, 但愿能给大师带来赞助!更多出色内容请实时存眷咱们应届毕业生测验网!
PHP中fopen,file_get_contents,curl函数的区分:
1.fopen /file_get_contents 每次要求城市从头做DNS查问,并毛病 DNS信息停止缓存。可是CURL会主动对DNS信息停止缓存。对统一域名下的网页或图片的要求只要要一次DNS查问。这大大削减了DNS查问的次数。以是CURL的机能比fopen /file_get_contents 好良多。
2.fopen /file_get_contents 在要求HTTP时,利用的是http_fopen_wrapper,不会keeplive。而curl却能够。如许在屡次要求多个链接时,curl效力会好一些。
3.fopen / file_get_contents 函数会遭到php.ini文件中allow_url_open选项设置装备摆设的影响。若是该设置装备摆设封闭了,则该函数也就生效了。而curl不受该设置装备摆设的影响。
4.curl 能够摹拟多种要求,比方:POST数据,表单提交等,用户能够根据本身的须要来定制要求。而fopen / file_get_contents只能利用get体例获得数据。
file_get_contents 获得长途文件时会把成果都存在一个字符串中 fiels函数则会贮存成数组情势
是以,我还是比拟偏向于利用curl来拜候长途url。Php有curl模块扩大,功效非常壮大。
说了半天大师能够说机能怎样没对照呢,那咱们就来看看
比来须要获得别人网站上的音乐数据。用了file_get_contents函数,可是老是会碰到获得失利的题目,虽然根据手册中的 例子设置了超时,可大都时辰不会见效:
$config['context'] = stream_context_create(array('http' => array('method' => "GET",
'timeout' => 5//这个超不时辰不不变,常常不见效
)
));
这时辰辰,看一下办事器的毗连池,会发明一堆近似的毛病,让我头疼万分:
file_get_contents(//***): failed to open stream…
此刻改用了curl库,写了一个函数替代:
function curl_file_get_contents($durl){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $durl);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
curl_setopt($ch, CURLOPT_REFERER,_REFERER_);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$r = curl_exec($ch);
curl_close($ch);
return $r;
}
如斯,除真实的收集题目外,没再呈现任何题目。
这是别人做过的对于curl和file_get_contents的测试:
file_get_contents抓取google.com需用秒数:
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
curl利用的时辰:
0.68719101
0.64675593
0.64326
0.81983113
0.63956594
差别很大?呵呵,从我利用的经历来讲,这两个东西不只是速率有差别,不变性也相差很大。
倡议对收集数据抓取不变性要求比拟高的伴侣利用下面的 curl_file_get_contents函数,岂但不变速率快,还能冒充阅读器棍骗方针地点哦
再看一个实例
后续贴出了curl和file_get_contents的对照成果,这边除curl与file_get_contents的机能对照,还包罗了他们的机能对照,讲之前看下以下的成果图:
curl与file_get_contents机能对照PHP源代码以下:
/**
* 经由过程淘宝IP接口获得IP地舆地位
* @param string $ip
* @return: string
**/
function getCityCurl($ip)
{
$url="//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="//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")."
";
}
$endTime = explode(' ',microtime());
$endTime = $endTime[0] + $endTime[1];
$totalTime = $endTime - $startTime;
echo 'file_get_contents:'.number_format($totalTime, 10, '.', "")." seconds
";
//for curl
$startTime2=explode(' ',microtime());
$startTime2=$startTime2[0] + $startTime2[1];
for($i=1;$i<=10;$i++)
{
echo getCityCurl('121.207.247.202')."
";
}
$endTime2 = explode(' ',microtime());
$endTime2=$endTime2[0] + $endTime2[1];
$totalTime2 = $endTime2 - $startTime2;
echo "curl:".number_format($totalTime2, 10, '.', "")." seconds";
?>
测试拜候
//www.jb51.net
file_get_contents速率:4.2404510975 seconds
curl速率:2.8205530643 seconds
curl比file_get_contents速率快了30%摆布,最重要的是办事器负载更低.
总结
file_get_contents处置频仍小的时辰,用它感受挺好的。没甚么非常。若是你的文件被1k+人处置。那末你的办事器cpu就等着高升吧。以是倡议本身和大师在今后写php代码的时辰利用curl库。
【php中file-get-contents与curl机能比拟阐发】相干文章:
php的curl进修总结03-31
PHP前端开辟中的机能03-28
php中援用的用法阐发04-01
PHP顶用CURL捏造IP来历的方式03-31
PHP若何利用curl完成数据抓取03-24