file_get_contents or curl in php?
Solution 1
First of all cURL has a lot of options to set. You can really set any option you need to - many supported protocols, file-uploads, cookies, proxies and more.
file_get_contents()
really just GETs or POSTs the file and has the result.
However: I tried some APIs and did some "benchmarking":
cURL was a lot faster than file_get_contents
Just try it with your terminal: time php curl.php
curl.php:
<?php
$ch = curl_init();
$options = [
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => 'http://api.local/all'
];
curl_setopt_array($ch, $options);
$data = json_decode(curl_exec($ch));
curl_close($ch);
fgc.php
<?php
$data = json_decode(file_get_contents('http://api.local/all'));
Averaged cURL was 3-10 times faster than file_get_contents
in my case. The api.local responeded with a cached JSON file - about 600kb.
I don't think it was coincidence - But that you can't measure this accurately, because the network and the response times differ a lot, based on their current load / network speed / response times etc. (local networks won't change the effect - there will be load & traffic too)
But for certain use cases, it could also be that file_get_contents
is actually quicker.
So I built a simple function: https://git.io/J6s9e
Solution 2
Curl
is faster then File_get_contents
. I just did some quick bench-marking on this.
Fetching google.com using file_get_contents took (in seconds):
2.31319094
2.30374217
2.21512604
3.30553889
2.30124092
CURL took:
0.68719101
0.64675593
0.64326
0.81983113
0.63956594
Solution 3
For your information, curl can you let have more options and use GET/POST method and send parameters.
And file_get_contents
will have lesser options for you to GET/POST parameters.
Hope this helps...
CJ7
Updated on October 20, 2021Comments
-
CJ7 over 2 years
Which of
file_get_contents
orcurl
should be used in PHP to make an HTTP request?If
file_get_contents
will do the job, is there any need to usecurl
? Usingcurl
seems to need more lines.eg:
curl:
$ch = curl_init('http://www.website.com/myfile.php'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $content); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec ($ch); curl_close ($ch);
file_get_contents:
$output = file_get_contents('http://www.website.com/myfile.php'.$content);
-
Admin over 11 yearscurl can do a whole lot more than file_get_contents(), but if you don't need anything it does, then take the simpler approach.
-
ankur140290 over 11 yearsI have heard that, using file_get_contents have a few security threats and so few servers disable this feature in PHP.
-
Admin over 11 yearsheard where? citation please. some hosts disable all methods of getting external files, if file_get_contents is disables, you an be sure curl would be also.
-
ankur140290 over 11 years@Dagon phpsec.org/projects/phpsecinfo/tests/allow_url_fopen.html This is the first place I had read about the security issue. Also, cURL seems faster than file_get_contents. And here is a nice post about the same -> stackoverflow.com/questions/555523/…
-
Stegrex over 11 years@Dagon At my old job, our enterprise PHP package had allow_url_fopen disabled, so we had to use cURL instead when scraping web services. Not sure what the specific issue was, but with cURL you can do things like pass in login information in a post and work with the returned data more flexibly than with file_get_contents.
-
Admin over 11 years@teami thats specific to include() and require(), not the op's issue of file_get_contents
-
Admin over 11 years@Stegrex wow who ever set up the package was clearly clueless then :-)
-
Chris Rutledge about 5 yearsYour cURL example is creating a POST request, your file_get_contents is creating a GET request, so not a fair comparison
-
PHP Guru over 3 yearsIt's also noteworthy that allow_url_fopen might sometimes be disabled in php.ini for security reasons, and that would prevent file_get_contents from being used to open URLs.
-
-
GitaarLAB over 6 yearsAre these testresults (in seconds) that you posted really the results that you got in your test? The difference seems a bit large.. what php-version where you using?