Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Comparative Analysis of the performance of file_get_contents and curl in php

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the comparative analysis of the performance of file_get_contents and curl in php". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the comparative analysis of the performance of file_get_contents and curl in php".

In php, if you do not carefully analyze the performance, you will find that file_get_contents and curl have a lot in common, they can collect files and open files, but if you compare them carefully, you will find many differences. Let's take a look at the difference between file_get_contents and curl.

The difference between the fopen,file_get_contents,curl function in PHP:

1.fopen / file_get_contents redoes the DNS query with each request, and the DNS information is not cached. However, CURL automatically caches DNS information. Requests for web pages or images under the same domain name need only one DNS query. This greatly 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 instead of keeplive when requesting HTTP. And curl can. This makes curl more efficient when multiple links are requested multiple times.

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 becomes invalid. Curl is not affected by this configuration.

4.curl can simulate a variety of requests, such as POST data, form submission, etc., and users can customize the request according to their own needs. On the other hand, fopen / file_get_contents can only use get to obtain data.

When file_get_contents acquires a remote file, the result is stored in a string and the fiels function is stored as an array.

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

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

Recently, I need to get music data from other people's websites. The file_get_contents function is used, but the problem of getting failed is always encountered, and although the timeout is set according to the examples in the manual, it doesn't work most of the time:

The copy code is as follows:

$config ['context'] = stream_context_create (array (' http' = > array ('method' = > "GET")

'timeout' = > 5pm / this timeout is unstable and often does not work

)

))

At this time, if you look at the connection pool of the server, you will find a bunch of similar errors, which give me a terrible headache:

File_get_contents (http://***): failed to open stream...

Now I use the curl library instead, and write a function replacement:

The copy code is 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

}

In this way, there are no more problems except for the real network problems.

This is a test that others have done about curl and file_get_contents:

The number of seconds it takes for file_get_contents to grab 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? Ha ha, from my experience, the two tools are not only different in speed, but also in stability.

It is suggested that friends who have high requirements for network data grabbing stability should use the above curl_file_get_contents function, which is not only stable and fast, but also can impersonate a browser to deceive the target address.

Look at another example.

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

Performance comparison between curl and file_get_contents the PHP source code is as follows:

The copy code is as follows:

Test access

Https://www.jb51.net

File_get_contents speed: 4.2404510975 seconds

Curl speed: 2.8205530643 seconds

Curl is about 30% faster than file_get_contents, and the most important thing is that the server load is lower.

Thank you for your reading, the above is the content of "Comparative Analysis of file_get_contents and curl performance in php". After the study of this article, I believe you have a deeper understanding of the comparative analysis of file_get_contents and curl performance in php, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report