Efficiency Analysis of file_get_content curl and fopen in php

  • 2021-07-18 07:40:29
  • OfStack

Although all three functions are functions for reading resources, their application scenarios are different.

curl is mostly used for crawling between Internet pages, fopen is mostly used for reading files, and file_get_contents is mostly used for fetching the contents of static pages.

1. fopen/file_get_contents Redo the DNS query with each request and do not cache the DNS information. However, CURL automatically caches DNS information. Only one DNS query is required for the request of web pages or pictures under the same domain name. This greatly reduces the number of DNS queries. Therefore, the performance of CURL is much better than that of fopen/file_get_contents.

2. fopen/file_get_contents When requesting HTTP, http_fopen_wrapper is used instead of keeplive. But curl can. In this way, when multiple links are requested many times, curl efficiency will be better.

3. curl can simulate a variety of requests, such as POST data, form submission, etc. Users can customize requests according to their own needs. However, fopen/file_get_contents can only use get to obtain data.


Related articles: