Deru Knowledgebase

Email to friend
* Your name:
* Your email:
* Friend's email:
Comment:


How to use curl to get the file contents

You can use curl to do similar work of fopen and file_get_contents.

Some of the php functions like allow_url_fopen, shell_exec are dangerous functions and it will be disabled in most of the php settings.
It is better to use the curl functions to do the corresponding operations.

Here I shall mention about the replacement of php function file_get_contents with newly created curl function file_get_contents_curl.

1) First of all we need to create the function file_get_contents_curl
function file_get_contents_curl($url) {
    $ch = curl_init();
 
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
    curl_setopt($ch, CURLOPT_URL, $url);
 
    $data = curl_exec($ch);
    curl_close($ch);
 
    return $data;
}


The above code can be included in respective php file after the require function.

2) We need to use this file_get_contents_curl function to do the corresponding action of file_get_contents.

You can replace following code:
$response = file_get_contents($request);
to
$response = file_get_contents_curl($request);

This will do the same work of file_get_contents.



RSS