Home  >  Article  >  Backend Development  >  PHP fopen/file_get_contents and curl performance comparison

PHP fopen/file_get_contents and curl performance comparison

藏色散人
藏色散人forward
2019-07-10 14:16:203922browse

The difference between fopen, file_get_contents, and curl functions in PHP:

1.fopen/file_get_contents will re-do the DNS query for each request and does not cache the DNS information.

But CURL will automatically cache DNS information. Requests for web pages or images under the same domain name only require one DNS query. This significantly reduces the number of DNS queries. So the performance of CURL is much better than fopen /file_get_contents.

2.fopen/file_get_contents uses http_fopen_wrapper when requesting HTTP and will not keeplive.

But curl can. In this way, curl will be more efficient when requesting multiple links multiple times.

3. The fopen/file_get_contents function will be affected by the allow_url_open option configuration in the php.ini file.

If the configuration is turned off, this function will be invalid. Curl is not affected by this configuration.

4.curl can simulate a variety of requests, such as POST data, form submission, etc. Users can customize requests according to their own needs.

And fopen /file_get_contents can only use the get method to obtain data.

file_get_contents When retrieving remote files, the results will be stored in a string. The fiels function will store them in array form.

Therefore, I still prefer to use curl to access the remote url. Php has the curl module extension, which is very powerful.

After talking for a long time, everyone may say that there is no comparison in performance, so let’s take a look.

#最近需要获取别人网站上的音乐数据。用了file_get_contents函数,但是总是会遇到获取失败的问题,尽管按照手册中的 例子设置了超时,可多数时候不会奏效:
$config['context'] = stream_context_create(array(‘http’ => array(‘method’ => “GET”,
   ’timeout’ => 5//这个超时时间不稳定,经常不奏效
   )
  ));
#这时候,看一下服务器的连接池,会发现一堆类似的错误,让我头疼万分:
file_get_contents(http://***): 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;
}

In this way, except for real network problems, no problems have occurred.

This is a test done by others about curl and file_get_contents:

The number of seconds it takes file_get_contents to crawl google.com:

2.31319094
2.30374217
2.21512604
3.30553889
2.30124092

Time used by curl:

0.68719101
0.64675593
0.64326
0.81983113
0.63956594

Is there a big gap?

Haha, from my experience, these two tools are not only different in speed, but also in stability.

It is recommended that friends who have high requirements for the stability of network data capture use the curl_file_get_contents function above. It is not only stable and fast, but also can fake the browser to deceive the target address.

Let’s look at another example

The comparison results of curl and file_get_contents are posted later. In addition to the performance comparison of curl and file_get_contents, it also includes their performance comparison. Before talking about it, take a look at the following result chart:

curl Performance comparison with file_get_contents PHP source code is as follows:

<?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==&#39;1&#39;){ 
        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==&#39;1&#39;){ 
        return false; 
    } 
    $city = $ipinfo->data->region.$ipinfo->data->city; 
    return $city; 
} 
// for file_get_contents 
$startTime=explode(&#39; &#39;,microtime()); 
$startTime=$startTime[0] + $startTime[1]; 
for($i=1;$i<=10;$i++) 
{ 
   echo getCity("121.207.247.202")."</br>"; 
} 
$endTime = explode(&#39; &#39;,microtime()); 
$endTime = $endTime[0] + $endTime[1]; 
$totalTime = $endTime - $startTime; 
echo &#39;file_get_contents:&#39;.number_format($totalTime, 10, &#39;.&#39;, "")." seconds</br>"; 
//for curl 
$startTime2=explode(&#39; &#39;,microtime()); 
$startTime2=$startTime2[0] + $startTime2[1]; 
for($i=1;$i<=10;$i++) 
{ 
   echo getCityCurl(&#39;121.207.247.202&#39;)."</br>"; 
} 
$endTime2 = explode(&#39; &#39;,microtime()); 
$endTime2=$endTime2[0] + $endTime2[1]; 
$totalTime2 = $endTime2 - $startTime2; 
echo "curl:".number_format($totalTime2, 10, &#39;.&#39;, "")." seconds"; 
?>

file_get_contents speed: 4.2404510975 seconds

curl speed: 2.8205530643 seconds

curl is about 30% faster than file_get_contents, the most important thing What's more, the server load is lower.

Summary

file_get_contents It feels good to use it when the processing of file_get_contents is frequent and small. Nothing unusual. If your file is processed by 1k people. Then your server CPU is waiting to be upgraded. Therefore, I recommend that I and everyone use the curl library when writing PHP code in the future.

The above is the detailed content of PHP fopen/file_get_contents and curl performance comparison. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete