Comparison between the curl function and the file_get_content function in php for page capture

Source: Internet
Author: User
Tags curl ftp

It wasn't until recently that file_get_content was completely unable to meet the requirements when I was planning a web page thief program. I think file_get_content is easier to use than curl when reading remote content.


Comparison between curl and file_get_content in php


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: Copy code

$ My_curl = curl_init (); // initialize a curl object

Curl_setopt ($ my_curl, CURLOPT_URL, "http://www.111cn.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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.