The difference between curl, fopen and file_get_contents
curl is mostly used to crawl Internet web pages, fopen is mostly used to read files, and file_get_contents is mostly used to obtain the content of static pages.
1. fopen /file_get_contents The DNS query will be re-done every time and the DNS information will not be cached. However, 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 better than fopen /file_get_contents Much better.
2.fopen /file_get_contentsWhen requesting HTTP, http_fopen_wrapper will not keeplive . But curl can. In this way, curl will be more efficient when requesting multiple links multiple times.
3. 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.
I discovered after studying that curl supports many protocols, including FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP. In other words, it can do many things that file_get_content cannot do. Curl can achieve remote acquisition and collection of content in PHP; implement FTP upload and download of PHP web version; implement simulated login; implement interface docking (API), data transmission; implement simulated cookies; download file breakpoint resume transfer, etc., the function is very powerful .
After understanding some basic uses of curl, I found that it is not difficult. It is just a little difficult to remember some of the setting parameters, but we can just remember a few commonly used ones.
Enable curl:
Because PHP does not support the curl function by default, so if you want to use curl, you first need to enable this function in php.ini, that is, remove the semicolon in front of ;extension= php_curl.dll, and then save Then restart apache/iis and it will be fine.
<precourier new border-width:1px border-style:solid border-color:rgb rgb background-color:rgb padding:5px><span><?</span><span>php</span><span> $curl </span><span>=</span><span> curl_init</span><span>();</span><span>//初始化一个cURL对象</span><span> $url </span><span>=</span><span></span><span>"http://cart.jd.com/cart/cart.html?backurl=http://item.jd.com/176166.html&rid=0.9533184533 938766"</span><span>;</span><span> $header </span><span>=</span><span> array</span><span>();</span><span> $header</span><span>[]</span><span></span><span>=</span><span></span><span>'User-Agent: 5.0 (iPhone; U; CPU iPhone OS 4_3 like Mac OS X; en-us)'</span><span>;</span><span> $header</span><span>[]</span><span></span><span>=</span><span></span><span>'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'</span><span>;</span><span> $header</span><span>[]=</span><span></span><span>'Accept-Encoding: gzip,deflate'</span><span>;</span><span> $header</span><span>[]=</span><span></span><span>'//可以根据需要增加header内容'</span><span>;</span><span> curl_setopt</span><span>(</span><span>$culr</span><span>,</span><span>CURLOPT_URL</span><span>,</span><span> $url</span><span>);</span><span>//设置你需要抓去的URL地址</span><span> curl_setopt</span><span>(</span><span>$curl</span><span>,</span><span>CURLOPT_HEADER</span><span>,</span><span>$header </span><span>);</span><span>//设置header</span><span> curl_setopt</span><span>(</span><span>$curl</span><span>,</span><span>CURLOPT_RETURNTRANSFER</span><span>,</span><span>1</span><span>);</span><span>//将结果返回输出到字符串</span><span> $str </span><span>=</span><span> curl_exec</span><span>(</span><span>$curl</span><span>);运行</span><span>cURL</span><span>,请求网页</span><span> curl_close</span><span>(</span><span>$curl</span><span>);</span><span>//关闭url请求</span><span></span><span>return</span><span> $str</span><span>;</span><span>//返回或者显示结果</span><span>?></span></precourier>
The above has introduced the differences between curl, fopen and file_get_contents, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Both curl and Pythonrequests are powerful tools for sending HTTP requests. While curl is a command-line tool that allows you to send requests directly from the terminal, Python's requests library provides a more programmatic way to send requests from Python code. The basic syntax for converting curl to Pythonrequestscurl command is as follows: curl[OPTIONS]URL When converting curl command to Python request, we need to convert the options and URL into Python code. Here is an example curlPOST command: curl-XPOST https://example.com/api

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

To update the curl version under Linux, you can follow the steps below: Check the current curl version: First, you need to determine the curl version installed in the current system. Open a terminal and execute the following command: curl --version This command will display the current curl version information. Confirm available curl version: Before updating curl, you need to confirm the latest version available. You can visit curl's official website (curl.haxx.se) or related software sources to find the latest version of curl. Download the curl source code: Using curl or a browser, download the source code file for the curl version of your choice (usually .tar.gz or .tar.bz2

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

From start to finish: How to use php extension cURL for HTTP requests Introduction: In web development, it is often necessary to communicate with third-party APIs or other remote servers. Using cURL to make HTTP requests is a common and powerful way. This article will introduce how to use PHP to extend cURL to perform HTTP requests, and provide some practical code examples. 1. Preparation First, make sure that php has the cURL extension installed. You can execute php-m|grepcurl on the command line to check

PHP8.1 released: Introducing curl for concurrent processing of multiple requests. Recently, PHP officially released the latest version of PHP8.1, which introduced an important feature: curl for concurrent processing of multiple requests. This new feature provides developers with a more efficient and flexible way to handle multiple HTTP requests, greatly improving performance and user experience. In previous versions, handling multiple requests often required creating multiple curl resources and using loops to send and receive data respectively. Although this method can achieve the purpose

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.
