Differences between file_get_contents () and curl

5

I'm doing a script to get content from a url and I found here , here and aqui what can be done with curl and with file_gets_contents() , but I'm not entirely clear about two things:

  • What are the differences between the two?
  • Which is the most appropriate to get html from a url to then analyze the html ?

The code I used is this:

file_get_contents

$html = file_get_contents($urlFlujo, false, $context);

curl

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);

They perform the same function as obtaining an html from an url, but I am not clear that they work in the same way or serve exactly the same.

I ask only empirical and demonstrable data of which form is the most appropriate (shorter execution time, less consumption of resources, etc.) for my case.

    
asked by Mariano 26.06.2017 в 10:57
source

2 answers

7

In a nutshell:

file_get_contents()

It's a simple screwdriver . Ideal for simple GET requests where the header, HTTP request method, waiting time, cookies, redirects and other more complex things do not matter.

They can also be used for POST requests, but you should create a context for it ( See this contribution in the PHP Manual ).

It's less secure than cURL and that's why it's disabled by default in many PHP environments.

cURL

It is much more powerful and for more advanced uses.

cURL is probably the most widely used HTTP library in the world of programming. It was originally encoded using C language, and then it was ported to many other languages.

cURL can handle complicated HTTP manipulations elegantly, such as asynchronous HTTP requests, progress reports, etc.

The only problem with cURL is that it requires some time, at least several hours, to become familiar with its functions and coding style.

But being a library so used, the documentation and examples of code in the network are plentiful.

As an example, just look at the extensive documentation for cURL in the PHP Manual (link above).

Which is faster?

All the tests I've read so far indicate that cURL is faster.

The following graphic is the product of a test between different methods, published by Philip Norton in Quickest Way To Download To Web Page With PHP

See also:

answered by 26.06.2017 / 11:26
source
1

They really work for the same thing, only that file_gets_contents is standard in PHP, and curl is a module that must be installed and activated in the PHP configuration.

curl has many more options, easier to use and there are many more examples on the internet.

With file_get_contents you have functions similar to curl , but you have to set up a context, with streams and other things that complicate a lot, at least at the beginning.

In any case, the main and most important difference is that the function file , which is used by file_get_contents , may be blocked in some shared web servers of some hostings , since certain uses of it can be a security problem. In case of being blocked you will have to use curl by obligation.

    
answered by 26.06.2017 в 11:28