I know it is an old topic but I believe this is really important. And now, there are a lot of differences more than 8 years ago. As we all know, Curl is 3rd part library.
Simple Comparison: Last version of Curl library has more than 170 different functions to be able to send proper request to APIs. There
were only 70 functions 8 years ago. Fact: still under development.
That's why I wanted to put a new comment to this question.
What is file_get_contents()
file_get_contents() is a filesystem function in PHP that you can read contents from a file and make requests using GET and POST methods. You can add parameters to your request while you're using file_get_contents() function. You can see the sample below.
$data = http_build_query(
array(
'user_id' => '558673',
'user_name' => 'John Doe'
)
);
$config = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-Type: application/x-www-form-urlencoded',
'content' => $data
)
);
$context = stream_context_create($config);
$result = file_get_contents('https://google.com', false, $context);
What is curl()
Curl is open source third party library. You can reach the git repo from here. This function "simulates" HTTP requests and responses. This simulation allows you handling async HTTP requests and complex data transfers. In addition, Curl is suitable for performing cross-domain based FTP request. It can be used in various apps like data crawling from a website and proxy setup.
Let's check a CURL request syntax.
$url = API_ENDPOINT."/get_movies";
$curl = curl_init();
$params = array(
'category' => $category,
'limit' => $limit,
'start' => $start,
'order' => $order,
'term' => $term
);
$params_string = http_build_query($params);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params_string);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($curl);
curl_close($curl);
echo json_decode($data,TRUE); //service returns json in this sample
Note: This is the basic sample of a curl request. You can add more parameter and options to the curl object using its functions like CURLOPT_HTTPHEADER, CURLOPT_SSL_VERIFYPEER. These kind of parameters all up to you and the service that you trying to use.
CURL vs file_get_contents()
CURL
is able to handle complex HTML communications, but
file_get_contents()
is not.
CURL
supports HTTP PUT, GET, POST but
file_get_contents()
supports simple HTTP GET and HTTP POST
requests.
CURL
supports caching and cookies but
file_get_contents()
doesn’t support caching, cookies, etc.
CURL
is able to use HTTP, HTTPS, FTP, FTPS and more.
file_get_contents()
uses HTTP and HTTPS protocols for
communications.
CURL
can be used to read, update and delete files
from server, but file_get_contents()
only allows you to read a
file.
CURL
is more secure and faster than file_get_contents()
CURL
is bit more complex to understand than file_get_contents()
.
cURL
is capable of much more thanfile_get_contents
. That should be enough. – Commandeer