Browse > Home / Webmaster Tutorials / Alternative for file_get_contents()

| Subcribe via RSS

Alternative for file_get_contents()

May 30th, 2009 Posted in Webmaster Tutorials

There are some shared webhosts that have the populair PHP function file_get_contents() disabled. Since there are alot of scripts that need to fetch data from a website it’s crucial that it works.

Therefor I offer the webmasters this alternative:

<?php
$file_contents = file_get_contents(’http://example.com/‘);

// display file
echo $file_contents;
?>

BECOMES:
<?php
$site_url = ‘http://example.com‘;
$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $site_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

ob_start();
curl_exec($ch);
curl_close($ch);
$file_contents = ob_get_contents();
ob_end_clean();

echo $file_contents;
?>

One Response to “Alternative for file_get_contents()”

  1. LiteWaerz Says:

    this should be turned into a function and should be coded better but the basic objective is there !


Leave a Reply

  • Website of the month

    URL: TemplateZ.org
    Type: vBulletin
    Graphics: 9/10
    Content: 10/10
    Overall: 9.5