Home  >  Article  >  Backend Development  >  The difference between fopen, file_get_contents, and curl functions in PHP, curlgetcontents_PHP tutorial

The difference between fopen, file_get_contents, and curl functions in PHP, curlgetcontents_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 09:00:54845browse

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

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

Overall

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

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1091265.htmlTechArticleThe difference between fopen, file_get_contents, and curl functions in PHP, curlgetcontents 1. fopen /file_get_contents will redo DNS for each request Query, DNS information is not cached. But CURL will automatically...
Statement:
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