Comparison and analysis of file_get_contents and curl performance in PHP _php skills

Source: Internet
Author: User
Tags curl explode get ip php source code

This article illustrates the comparison and analysis of file_get_contents and curl performance in PHP. Share to everyone for your reference. Specifically as follows:

In PHP if not careful to analyze performance will find that file_get_contents and curl two have a lot in common, they can collect file open file, but if a careful comparison will find a lot of different points, let's look at the file_get_ The difference between contents and curl.

The difference between Fopen,file_get_contents,curl functions in PHP:

1.fopen/file_get_contents DNS queries for each request, and does not cache DNS information. However, Curl will automatically cache DNS information. Requests for a Web page or picture under the same domain name require only a single DNS query. This greatly reduces the number of DNS queries. So curl performance is much better than fopen/file_get_contents.

2.fopen/file_get_contents when HTTP is requested, the Http_fopen_wrapper is used and does not keeplive. and curl can. This makes curl more efficient when multiple links are requested more than once.

The 3.fopen/file_get_contents function is affected by the configuration of the Allow_url_open option in the php.ini file. If the configuration is turned off, the function is invalidated. The 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 the request according to their own needs. Fopen/file_get_contents can only obtain data using GET.
File_get_contents Gets the remote file, the result is in a string, the Fiels function stores the array form

Therefore, I prefer to use curl to access remote URLs. PHP has a Curl module extension, the function is very powerful.

Said for a long time everybody may say the performance how does not compare, then we take a look

Recently you need to get the music data on someone's website. The File_get_contents function is used, but there is always a problem of getting a failure, although a timeout is set by the example in the manual, but most of the time it will not work:

Copy Code code as follows:
$config [' context '] = stream_context_create (Array (' HTTP ' => Array (') ' => ' get ',
' Timeout ' => 5//This timeout period is unstable and often does not work.
)
));

At this point, look at the connection pool of the server, you will find a bunch of similar errors, I have a headache:

File_get_contents (http://***): failed to open stream ...
Now instead of the Curl library, write a function substitution:

Copy Code code as follows:
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;
}

So, in addition to the real network problems, there is no more problems.
This is the test that someone else has done about curl and file_get_contents:
File_get_contents crawl google.com Required seconds:

2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
Curl Time to use:

0.68719101
0.64675593
0.64326
0.81983113
0.63956594

A big gap? Well, from the experience I used, these two tools are not only the speed difference, the stability is also very big difference.

It is recommended that the network data capture stability requirements of high friends use the above curl_file_get_contents function, not only stable speed, but also fake browser spoofing target address OH

Look at one more example

Later posted on the contrast between Curl and file_get_contents, this side in addition to the curl and file_get_contents performance comparison, but also contains their performance comparisons, said before to see the following results map:

Curl and file_get_contents Performance comparison PHP source code is as follows:

Copy Code code as follows:
<?php
/**

* Get IP location through Taobao IP interface

* @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, Ten, '. ', ""). "Seconds</br>";

For Curl
$startTime 2=explode (", Microtime ());
$startTime 2= $startTime 2[0] + $startTime 2[1];
For ($i =1 $i <=10; $i + +)
{
echo getcitycurl (' 121.207.247.202 '). " </br> ";
}
$endTime 2 = Explode (', microtime ());
$endTime 2= $endTime 2[0] + $endTime 2[1];
$totalTime 2 = $endTime 2-$startTime 2;
echo "Curl:" Number_format ($totalTime 2, '. ', ""). "Seconds";
?>

Test access
Http://www.jb51.net
File_get_contents Speed: 4.2404510975 seconds
Curl Speed: 2.8205530643 seconds
The curl is about 30% faster than the file_get_contents speed, and the most important is the lower server load.

Summarize

It feels good to use it when the file_get_contents is very often small. Nothing unusual. If your document is handled by a 1k+ person. Then your server CPU is waiting to go up. So I suggest myself and everyone in the future to write PHP code when using the Curl Library.

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.