The file_get_content function has always been used to capture remote content. In fact, the existence of such a good thing as curl has long been known. However, after a glance, it seems complicated to use, not as simple as file_get_content, that is, the requirement is not big, so I didn't learn to use curl until recently. when I had to create a web page thief program, I found that file_get_content was completely unable to meet the requirement. I think file_get_content is easier to use than curl when reading remote content.
Main differences:
After learning, we found that curl supports many protocols, including FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE, and LDAP. that is to say, it can do a lot of things that file_get_content cannot do. Curl can remotely obtain and collect content in php; implement FTP upload and download in PHP web version; implement simulated login; implement interface connection (API), data transmission; implement Cookie simulation; resumable Upload of downloaded files.
After learning about the basic usage of curl, we can find that it is not difficult, but it is difficult to remember some parameters, but we can remember several common ones.
Enable curl:
Because PHP does not support the curl function by default, if you want to use curl, you must first. enable this function in ini, that is, remove the semicolon before extension = php_curl.dll, and then save it and restart apache/iis.
Basic syntax:
The code is as follows:
$ My_curl = curl_init (); // initialize a curl object
Curl_setopt ($ my_curl, CURLOPT_URL, "http://www.jb51.net"); // Set the URL you want to crawl
Curl_setopt ($ my_curl, CURLOPT_RETURNTRANSFER, 1); // you can specify whether to save the result to a string or output it to the screen. 1 indicates to save the result to a string.
$ Str = curl_exec ($ curl); // execute the request
Echo $ str; // output the captured result
Curl_close ($ curl); // closes the url request