Difference between file_get_contents and Curl ?

Asked

Viewed 19,928 times

9



I wonder if there is any difference between using Curl and file_get_contents and which is safer ?
Thank you in advance.

  • 3

    In fact, it’s almost completely unrelated. They are completely different things, but as both can be used to access a URL (and this depends on the PHP configuration allow), Voce may in SOME cases use one or the other (but this is a specific case).

  • 2

    Note in the manual that the description of the function has nothing to do with what has been answered so far: http://php.net/manual/en/function.file-get-contents.php

2 answers

14


First of all file_get_contents is intended to read the contents of a file. But can also be used to read urls.

In PHP, there are some Wrappers (a definition similar to the http request protocol), where you can define what type of stream will be read by these functions.

For example, with file_get_contentscan read: data in memory, data sent via Http Post or Put, RAR extension file, and HTTP urls.

To illustrate, I show below how to read the data sent via POST or PUT, in an HTTP request. They can be accessed by file_get_contents and other PHP file reading functions (fopen or readfile).

Example:

$raw_http_post = file_get_contents('php://input');

See the list of Wrappers supported by PHP.

So it’s possible through wrapper http:// or https:// URL requests. This is not limited only to type requests GET, as quoted in the other reply, but for any type of HTTP request method, such as POST, PUT and the like.

In the latter case, you only need to add one context (through function stream_context_create) for the read function where you are reading the data of a url.

Example of request with context

// Desabilita a verificação de SSL

$config = array(
    "ssl"=> array(
        "verify_peer"=>false,
        "verify_peer_name"=>false,
    ),
);  

$context = stream_context_create($config);

file_get_contents("http://site.com.br/", false, $context);

CURL

According to the PHP manual:

PHP supports libcurl, a library created by Daniel Stenberg that allows you to connect and communicate with different types of servers using different types of protocols. libcurl currently supports http, https, ftp, Gopher, telnet, Dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP upload (can also be done with PHP ftp extension), HTTP upload per form, proxies, cookies, and user and password authentication.

That is, Curl contains for the purpose of making requests of various types. It is widely used to make requests to http and https protocols.

Him, unlike the file_get_contents, does not support system reading files from your server. This is a good point to note.

Advantages and Disadvantages

Permissions to read Urls

With the use of the read functions of file types in PHP, it is necessary that the directive allow_url_open is qualified in its php.ini. Without that you won’t be able to do this operation.

However, as the extension curl is prepared precisely for this type of request no reading restrictions urls.

Simultaneous requests

Another important point to highlight is that with file_get_contents you cannot make simultaneous requests, already with the Curl is possible.

I will explain what the simultaneous requisitions mean.

They are essential to reduce the processing time of your page.

Suppose when you request a PHP script, you need to make requests for 5 other pages. For each url you capture the request, it is necessary to wait for a response for the other operation to be done.

With simultaneous requests, the curl internally sends the requests, it is only necessary to wait for all responses to be completed.

See here an example of multiple requests with Curl

2

file_get_contents besides read all the contents of a file to a string, often is also used for simple requests to urls, although from php 4.3 we can also define other parameters of the request when these are needed there is always a tendency to use curl instead of the parameter context of file_get_contents

curl is a fully configurable tool, we can define all the options and more for a given request.

Regarding security, the service/url to which the request will be made, is curl or file_get_contents is that you have to worry about it. But for a simple request GET, just define that only a certain User-Agent (ex: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1) is that you can access to DEFAULT file_get_contents no longer working, there is the tendency to use curl and define a User-Agent in the request (although with file_get_contents also be possible by creating a stream and using as third parameter of function).

Example of a request/request GET with Curl defining a User-Agent:

function get_req($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, True);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl,CURLOPT_USERAGENT,'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1');
    $return = curl_exec($curl);
    curl_close($curl);
    return $return;
}

And we use it like this:

echo get_req('/questions/134738/diferen%C3%A7a-entre-file-get-contents-e-curl');

In my case, to GET simple experiment always first file_get_contents (simply by code reduction), in case some unexpected result returns then I orient myself using curl.

  • 1

    According to php.net website file_get_contents also requests the type POST ... with the settings made to stream_context_create.

  • You’re right, it must have happened in the latest versions of php: http://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents

  • Vlw Miguel, I could understand. As @João said, file_get_contents also accepts post, but I could understand between using one and the other.

  • Actually, people tend to use curl (because there are still more options). I will edit the answer correctly

  • @Miguel just did not give score yet because of this, look only since the version (PHP 4 >= 4.3.0, PHP 5, PHP 7) already has this feature. I agree to use Curl but if you make a statement like that someone can ask you a question! kkkk

  • 1

    file_get_contents opens any type of file and still stream supported by PHP. The beginning of the answer seems to lead the person to understand information in the wrong way

  • You’re right, as I’ve used php long before that, I didn’t know these new function parameters

  • file_gets_content() and type a Curl implementation in php

  • 2

    @Nikobellic nowhere near that. It’s just a function to read content from a/stream file and by chance, accesses Urls DEPENDING on how PHP was set up.

  • I can even this mistaken @Bacco but which URL does not reference any file?

  • @Nikobellic is essential that you do not confuse file systems with remote connections or anything like that. The url is a reference to a remote file (HTTP, HTTPS, FTP). When you use a path (path) file_get_contents will read the file on your server (not as if you were reading an "internal url", but rather the file on the disk)

  • @Nikobellic URL does not reference any file. If "by chance" the URL server picks up a file, it is particularity of the implementation. I can call a meusite.com/potato.jpg, and there is no file with that name on the machine, and even then, you get an image. It is not because two events usually happen together that means one is connected to the other.

  • nothing convincing this your comment @Wallace

  • 1

    @Nikobellic I have enough experience with PHP. I know I’m not making an irresponsible comment. You seem to be confusing file system with http requests and so on. file_get_contents When you use a "path" it doesn’t mean you are doing a "relative url", you are literally reading a file on disk. If it were in Windows for example, it would work perfectly: file_get_contents('c:\\temp.txt'), but can you do this by accessing a url? No. They are two different things, maybe confusing because php has decided to treat everything in the same function.

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.