Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using file get contents or curl

I was ask to use a simple facebook api to return the number of likes or shares at work which return json string. Now since i am going to do this for a very large amount of links, which one is better:

Using file_get_contents or cURL.

Both of them seem to return the same results and cURL seems to be more complicated to use, but what is the difference among them. why do most people recommend using cURL over file_get_contents? Before i run the api which might take a whole day to process, i will like to have feedback.

like image 887
Ibu Avatar asked Apr 30 '11 20:04

Ibu


People also ask

Which is faster cURL or file_get_contents?

file_get_contents() is slightly faster than cURL.

What is the difference between cURL and file_get_contents in PHP?

cURL is capable of much more than file_get_contents . That should be enough. FWIW there's little difference with regards to speed. I've just finished fetching 5,000 URLs and saving their HTML to files (about 200k per file).

What is the difference between the file () and file_get_contents () functions?

file — Reads entire file contents into an array of lines. file_get_contents — Reads entire file contents into a string.


1 Answers

A few years ago I benchmarked the two and CURL was faster. With CURL you create one CURL instance which can be used for every request, and it maps directly to the very fast libcurl library. Using file_get_contents you have the overhead of protocol wrappers and the initialization code getting executed for every single request.

I will dig out my benchmark script and run on PHP 5.3 but I suspect that CURL will still be faster.

like image 98
serby Avatar answered Sep 21 '22 12:09

serby