Skip to content
Related Articles

Related Articles

Improve Article

Difference between file_get_contents and cURL in PHP

  • Last Updated : 01 Jun, 2020

file_get_contents() Function: This PHP function is used to retrieve the contents of a file. The contents can be stored as a string variable. Alternatively, it also simulates HTTP transactions, involving requests via GET method and responses using POST method respectively. Primarily, it is best-suited for simple HTTP manipulations and to get single-line JSON responses. 

Example:

PHP




<?php 
  
// Reading contents from the 
// GeeksforGeeks homepage 
$homepage = file_get_contents(
  
echo $homepage
  
?>

Output: It will redirect to GeeksforGeeks home page.

cURL: It is a third party library that simulates HTTP requests and responses in a much more efficient way. It can handle asynchronous HTTP requests and complex communications like callback functions or break point continual transferring. It is also suitable for carrying out cross-domain based FTP request. Also, it can be used in different applications like proxy setup and Website Scarping etc. 

Example:
 

PHP




<?php 
  
// From URL to get webpage contents 
  
// Initialize a CURL session. 
$ch = curl_init(); 
  
// Return Page contents. 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  
// Grab URL and pass it to the variable 
curl_setopt($ch, CURLOPT_URL, $url); 
  
$result = curl_exec($ch); 
  
echo $result
  
?>

Output: It will redirect to GeeksforGeeks home page.

file_get_contents() MethodcURL
Handles simple HTTP communications.Handles complex HTTP communications.
Supports simple HTTP GET and HTTP POST operations.Supports HTTP PUT, certificates in addition to GET and POST requests.
Doesn’t support caching, cookies, etc.Supports caching, cookies progress reporting etc.
It uses HTTP and HTTPS protocols for communications.It uses HTTP, HTTPS, FTP, FTPS protocols.
It can be used to read the file content.It can be used to read, edit, update, delete files from server.
Slow in operation.Secure and fast in operation.
Easy to understand.Complex to understand.

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :