Skip to content

Instantly share code, notes, and snippets.

@Jako
Created November 16, 2016 14:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jako/c9ea82324c62c90c15ade95254ac57e1 to your computer and use it in GitHub Desktop.
Save Jako/c9ea82324c62c90c15ade95254ac57e1 to your computer and use it in GitHub Desktop.
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