Comparison between php file_get_contents and curl () functions

Source: Internet
Author: User

The steps to enable curl extension are as follows:

1. Copy the three php_curl.dll, libeay32.dll, and ssleay32.dll files in the PHP folder to system32;
2. Remove the semicolon from php. ini (c: WINDOWS directory) and extension = php_curl.dll;

3. Restart apache or IIS.

Let's take a look at the simple examples of two functions.

Curl () function

The code is as follows: Copy code

$ Ch = curl_init ("http://www.111cn.net /");
Curl_exec ($ ch );
Curl_close ($ ch );

// $ Ch = curl_init ("URL to be collected"); the role of the curl_init () function initializes a curl session

// Curl_exec ($ ch); execute $ ch

// Curl_close ($ ch); close $ ch

File_get_contents function

Example

The code is as follows: Copy code
<? Php
Echo file_get_contents ("http://www.111cn.net ");
?>

Output:

The code is as follows: Copy code

This is a test file with test text.

Summary

Fopen/file_get_contents performs a new DNS query every time a request is sent, and the DNS information is not cached.
However, CURL automatically caches DNS information. Only one DNS query is required for a webpage or image request under the same domain name. This greatly reduces the number of DNS queries.
Therefore, CURL performance is much better than fopen/file_get_contents.


File_get_contents and curl efficiency and stability

The code is as follows: Copy code

$ Config ['context'] = stream_context_create (array ('http' => array ('method' => "GET", 'timeout' => 5 )));

 
'Timeout' => 5 // The timeout time is unstable and is often difficult to use. At this time, let's take a look at the server's connection pool and we will find a bunch of errors similar to the following, which makes you have a headache:

The code is as follows: Copy code
File_get_contents (http: // ***): failed to open stream...

 

As a last resort, the curl Library is installed and a function replacement is written:

The code is as follows: Copy code

Function curl_get_contents ($ url)
{  
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url); // you can specify the url to access.
// Curl_setopt ($ ch, CURLOPT_HEADER, 1); // whether the header information is displayed
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 5); // set timeout
Curl_setopt ($ ch, CURLOPT_USERAGENT, _ USERAGENT _); // User Access Proxy User-Agent
Curl_setopt ($ ch, CURLOPT_REFERER, _ REFERER _); // Set referer
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1); // trace 301
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // return the result
$ R = curl_exec ($ ch );
Curl_close ($ ch );
Return $ r;
} 

In this way, there are no more problems except the real network problems.
This is a test on curl and file_get_contents that someone else has done:
File_get_contents:

The code is as follows: Copy code

1.2.31321394
2.2.30374217
3.2.21512604
4.3.30553889
5.2.30124092

Curl Time:

1.0.68719101
2.0.64675593
3.0.64326
4.0.81983113
5.0.63956594


So how can we use file_get_contents or curl () based on the server situation? Now we can use the function_exists function to determine whether php supports a function and easily write the following function:

The code is as follows: Copy code

<? Php
Function vita_get_url_content ($ url ){
If (function_exists ('File _ get_contents ')){
$ File_contents = file_get_contents ($ url );
} Else {
$ Ch = curl_init ();
$ Timeout = 5;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ File_contents = curl_exec ($ ch );
Curl_close ($ ch );
 }
Return $ file_contents;
 }
?>

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.