Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Recursive download with curl and open_basedir
/**
* Load data from URL with curl
*
* @param $url
* @return mixed
*/
public function loadData($url)
{
$curloptHeader = false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, $curloptHeader);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$safeMode = @ini_get('safe_mode');
$openBasedir = @ini_get('open_basedir');
if (empty($safeMode) && empty($openBasedir)) {
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
} else {
$redirects = 0;
$data = $this->curl_redirect_exec($ch, $redirects, $curloptHeader);
}
curl_close($ch);
return $data;
}
/**
* Recursive cURL with redirect and open_basedir
* http://stackoverflow.com/questions/3890631/php-curl-with-curlopt-followlocation-error
*
* @param $ch
* @param $redirects
* @param bool $curlopt_header
* @return mixed
*/
private function curl_redirect_exec(
$ch,
&$redirects,
$curlopt_header = false
) {
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
list($header) = explode("\r\n\r\n", $data, 2);
$matches = array();
preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches);
$url = trim(str_replace($matches[1], "", $matches[0]));
$url_parsed = parse_url($url);
if (isset($url_parsed)) {
curl_setopt($ch, CURLOPT_URL, $url);
$redirects++;
return $this->curl_redirect_exec($ch, $redirects, $curlopt_header);
}
}
if ($curlopt_header) {
return $data;
} else {
list(, $body) = explode("\r\n\r\n", $data, 2);
return $body;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.