SoFunction
Updated on 2025-03-09

The difference between curl and file_get_content in php

Until recently, when I was going to build a web thief program, I found that file_get_content could not meet the needs at all. I think that when reading remote content, file_get_content is not as good as curl except for using it conveniently than curl.


Key differences:

After learning, I found that curl supports many protocols, including FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE and LDAP. In other words, it can do a lot of things that file_get_content cannot do. Curl can remotely obtain and collect content in PHP web version; realize FTP upload and download of PHP web version; realize simulated login; implement interface docking (API), data transmission; implement simulated cookies; download file breakpoints and other functions, and are very powerful.

After understanding some basic use of curl, I found that it is not difficult, but remember some settings parameters in it, which is a little difficult to do, but we just need to remember a few commonly used ones.

Turn on curl:

Because PHP does not support curl function by default, if you want to use curl, you first need to enable the function in it, that is, remove the semicolon in front of ;extension= php_curl.dll, and then save it and restart apache/iis.

Basic syntax:

Copy the codeThe code is as follows:

$my_curl = curl_init();    //Initialize a curl object
curl_setopt($my_curl, CURLOPT_URL, "https://");     //Set the URL you need to crawl
curl_setopt($my_curl,CURLOPT_RETURNTRANSFER,1);     //Set whether to save the result to the string or output it to the screen, 1 means to save the result to the string
$str = curl_exec($curl);    //Execute the request
echo $str;    //Output the crawling result
curl_close($curl);    //Close the url request