Home > Backend Development > PHP Tutorial > The difference between fopen, file_get_contents and curl functions in PHP

The difference between fopen, file_get_contents and curl functions in PHP

WBOY
Release: 2016-07-29 09:06:35
Original
991 people have browsed it

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.
file_get_contents will store the results in a string when retrieving remote files, and the fiels function will store them in an array

In general

When file_get_contents is processed frequently and small, it feels good to use it. Nothing unusual. If your file is processed by 1k+ people. Then your server CPU is waiting for a boost. Therefore, I recommend that you and everyone use the curl library when writing PHP code in the future.

Original address: http://www.jb51.net/article/57238.htm

The above has introduced the differences between fopen, file_get_contents, and curl functions in PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template