Deru Knowledgebase
Search:     Advanced search
Browse by category:
Contact Us

How to use curl to get the file contents

Add comment
Views: 1224
Votes: 0
Comments: 0
Posted: 11 Aug, 2009
by: Joseph S.
Updated: 27 Aug, 2010
by: Joseph S.
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.
Others in this Category
document Running php4 and php5 together in server
document changing php values using .htaccess
document Track the "nobody mail senders" using PHP
document Using Curl to call a URL
document use Curl to POST data to a form.
document Fixing libxml, php bug and issues with HTML entities by downgrading libxml
document Steps to install FFMPEG as an module in PHP
document Mambo Admin "All contents Item" - join error
document How to install SSH2 for PHP Shell?
document Album access issue in Gallery
document How to install APC (Alternative PHP Cache)
document Upgrade from PHP4 to PHP5: Backward Incompatible Changes
document CAPCHA implementation in the Contact Page submission form



RSS