Skip to content

Instantly share code, notes, and snippets.

@micah1701
Created October 3, 2012 18:22
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 micah1701/3828780 to your computer and use it in GitHub Desktop.
Save micah1701/3828780 to your computer and use it in GitHub Desktop.
Recursive cURL function to follow links when local server does not allow CURLOPT_FOLLOWLOCATION
/**
* use cURL to get the contents on a remote page uses redirect headers
* necessary when local server does not allow location
*/
function curl($url,$recursive_attempt=0){
if (!function_exists('curl_init')) { return false; }
$content = false;
$can_follow = ( ini_get('safe_mode') || ini_get('open_basedir') ) ? false : true; //cURL can't follow redirects in safe mode or if open_basedir is on
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // set the URL
curl_setopt($ch, CURLOPT_HEADER, 1); // 0 = don't return header data, 1 = do return header data (needed if $can_follow == false)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return value instead of printing the response in the browser
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // fake a user agent so the fetch URL thinks this script is a browser
curl_setopt($ch, CURLOPT_TIMEOUT, 2); // timeout if the whole process takes longer than this many seconds
curl_setopt($ch, 156, 2000); // try to connect to URL for this many miliseconds
if($can_follow){
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow any redirect the accepting URL fowards to
}
$content = curl_exec($ch);
// if script could not follow redirect, get Location of where redirect wanted to go and then re-run this function with new URL
if(!$can_follow){
$header = curl_getinfo( $ch );
$recursive_attempt++;
if($header['http_code'] >= 300 && $header['http_code'] < 400 && $recursive_attempt < 3 ){
preg_match("#Location:\s(http:\/\/[^\s]*)#is", $content, $location); //get the location that the page tried to redirect to
$url = urldecode($location[1]);
curl_close($ch);
return $this->curl($url,$recursive_attempt);// re-call this same function recursivly with new $url
}
}
curl_close($ch);
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment