Curl, fopen, and file_get_contents differences

Source: Internet
Author: User
Curl is used for crawling between internet pages, fopen is used to read files, and file_get_contents is used to get the content of static pages.

1. fopen/file_get_contents Each request will be re-made DNS query, and does not cache the DNS information. However , CURL automatically caches the DNS information. Requests for Web pages or images under the same domain name require only one DNS query. This greatly reduces The number of DNS queries. So CURL has a much better performance than fopen/file_get_contents .

2. when requesting HTTP , the fopen/file_get_contents is using the Http_fopen_wrapper, not keeplive. and Curl can. This makes curl more efficient when multiple links are requested more than once .

3. Curl can simulate a variety of requests, such as post data, form submission, and so on, users can customize the request according to their own needs. Instead, fopen/file_get_contents can only get data using the Get method.


Learn to find that Curl supports a lot of protocols, FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, file, and LDAP, which means it can do a lot of things file_get_content can't. Curl in PHP can achieve remote access and acquisition of content, the implementation of PHP Web version of the FTP upload and download, to achieve the simulation of landing, interface docking (API), data transmission, implementation of analog cookies, download file breakpoints and so on, the function is very powerful.

Understanding curl Some basic use, only to find that it is not difficult, but to remember some of the settings in the parameters, difficult to get a little, but we remember a few commonly used on it.

Turn on Curl:

Because PHP does not support the Curl function by default, so if you want to use curl, you first need to open the function in php.ini, that is, to remove the extension= Php_curl.dll front of the semicolon, and then save and then restart Apache/iis.

 
  
   PHP$curl=Curl_init();//Initialize a Curl object$url="http://cart.jd.com/cart/cart.html?backurl=http://item.jd.com/176166.html&rid=0.9533184533 938766" ;$header=Array();$header[]=' user-agent:5.0 (iPhone; U CPU iPhone os 4_3 like Mac os X; En-US) ';$header[]=' accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ';$header[]=' Accept-encoding:gzip,deflate ';$header[]='//can add header content as needed ';curl_setopt($CULR,Curlopt_url,$url);//Set the URL address you need to grabcurl_setopt($curl,Curlopt_header,$header);//Set Headercurl_setopt($curl,Curlopt_returntransfer,1);//Outputs the result to a string$str=curl_exec($curl); RunCURL, request a Web pageCurl_close($curl);//Close URL requestreturn$str;//Return or show results?>
 


The above describes Curl, fopen and file_get_contents differences, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.