PHP, cURL, CURLOPT FOLLOWLOCATION and open basedir Or Safe Mode
July 18th, 2008Read More curl
If you are trying to get a curl script which needs follow on location functionality to run on a server which has either open_basedir or safe mode enabled you will get an error message similar to the following:
CURLOPT_FOLLOWLOCATION cannot be activated when in safe_mode or an open_basedir is set
After a bit of digging, some kind soul has put a workaround here
Here is how to use the function
PHP:
-
function curl($url){
-
curl_setopt ($go, CURLOPT_URL, $url);
-
//follow on location problems
-
-
-
curl_setopt ($go, CURLOPT_FOLLOWLOCATION, $l);
-
-
$syn = curl_exec($go);
-
-
}else{
-
-
$syn = curl_redir_exec($go);
-
-
}
-
-
curl_close($go);
-
return $syn;
-
}
-
-
//follow on location problems workaround
-
-
function curl_redir_exec($ch)
-
-
{
-
-
-
-
if ($curl_loops++>= $curl_max_loops)
-
-
{
-
-
$curl_loops = 0;
-
-
return FALSE;
-
-
}
-
-
curl_setopt($ch, CURLOPT_HEADER, true);
-
-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
-
$data = curl_exec($ch);
-
-
-
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
-
-
if ($http_code == 301 || $http_code == 302)
-
-
{
-
-
-
-
-
if (!$url)
-
-
{
-
-
//couldn't process the url to redirect to
-
-
$curl_loops = 0;
-
-
return $data;
-
-
}
-
-
-
if (!$url['scheme'])
-
-
$url['scheme'] = $last_url['scheme'];
-
-
if (!$url['host'])
-
-
$url['host'] = $last_url['host'];
-
-
if (!$url['path'])
-
-
$url['path'] = $last_url['path'];
-
-
$new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . ($url['query']?'?'.$url['query']:'');
-
-
curl_setopt($ch, CURLOPT_URL, $new_url);
-
-
return curl_redir_exec($ch);
-
-
} else {
-
-
$curl_loops=0;
-
-
return $data;
-
-
}
-
-
}











































