Skip to content

Instantly share code, notes, and snippets.

@mrclay
Created October 7, 2011 19:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrclay/1271106 to your computer and use it in GitHub Desktop.
Save mrclay/1271106 to your computer and use it in GitHub Desktop.
file_get_contents with fallback to cURL
<?php
function fetchUrl($url) {
$allowUrlFopen = preg_match('/1|yes|on|true/i', ini_get('allow_url_fopen'));
if ($allowUrlFopen) {
return file_get_contents($url);
} elseif (function_exists('curl_init')) {
$c = curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($c);
curl_close($c);
if (is_string($contents)) {
return $contents;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment