The example in this article describes the comparative analysis of the performance of file_get_contents and curl in php. Share it with everyone for your reference. The details are as follows:
If you don’t analyze the performance carefully in PHP, you will find that file_get_contents and curl have a lot in common. They can both collect files and open files. However, if you compare them carefully, you will find many differences. Let’s take a look at them together. See the difference between file_get_contents and curl.
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 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 When requesting HTTP, http_fopen_wrapper is used and will not keeplive. But curl can. In this way, curl will be more efficient when requesting multiple links multiple times.
3.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 disabled. 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.
When file_get_contents obtains remote files, the results will be stored in a string. The fiels function will store them in an array form
Therefore, I still prefer to use curl to access remote url. Php has curl module extension, which is very powerful.
After talking for a long time, you may say that there is no comparison in performance, so let’s take a look
Recently I need to obtain music data from other people’s websites. I used the file_get_contents function, but I always encountered the problem of failure to obtain it. Although I set the timeout according to the examples in the manual, it does not work most of the time:
file_get_contents(http://***): failed to open stream…
Now I use the curl library and write a function replacement:
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 spoof 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 of curl and file_get_contents, it also includes their performance comparison. Before talking about it, take a look at the following result chart:
Performance comparison between curl and file_get_contents PHP source code is as follows:
总结
file_get_contents处理频繁小的时候,用它感觉挺好的。没什么异常。如果你的文件被1k+人处理。那么你的服务器cpu就等着高升吧。所以建议自己和大家在以后写php代码的时候使用curl库。
希望本文所述对大家的PHP程序设计有所帮助。