Tuesday, August 25, 2009

File_Get_Contents replacement (CURL)

Some servers have file_get_contents function disabled. But don't worry, most of them have curl library installed. Here is a replacement function for file_get_contents, using CURL library. Easy and working:

  1. function file_get_contents_curl($url) {
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_HEADER, 0);
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  5. curl_setopt($ch, CURLOPT_URL, $url);
  6. $data = curl_exec($ch);
  7. curl_close($ch);
  8. return $data;
  9. }



Using this function is easy, just like file_get_contents function :

$contents = file_get_contents_curl( 'http://...');

No comments: