Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
//TODO get URL from parameters and authenticate it with an expiring hash/timestamp
$url = 'http://traffic.libsyn.com/atpfm/atp95.mp3';
// Forward these headers from original request
$requestHeaders = [];
if (isset($_SERVER['HTTP_USER_AGENT'])) $requestHeaders[] = 'User-Agent: ' . $_SERVER['HTTP_USER_AGENT'];
if (isset($_SERVER['HTTP_RANGE'])) $requestHeaders[] = 'Range: ' . $_SERVER['HTTP_RANGE'];
if (isset($_SERVER['HTTP_REFERER'])) $requestHeaders[] = 'Referer: ' . $_SERVER['HTTP_REFERER'];
if (isset($_SERVER['HTTP_IF_NONE_MATCH'])) $requestHeaders[] = 'If-None-Match: ' . $_SERVER['HTTP_IF_NONE_MATCH'];
if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) $requestHeaders[] = 'If-Modified-Since: ' . $_SERVER['HTTP_IF_MODIFIED_SINCE'];
// Forcibly add these (overwriting any previous values)
$requestHeaders[] = 'X-Forwarded-For: ' . $_SERVER['REMOTE_IP'];
$requestHeaders[] = 'X-Real-IP: ' . $_SERVER['REMOTE_IP'];
$isHeadRequest = $_SERVER['REQUEST_METHOD'] == 'HEAD';
$sentStatus = 0;
$c = curl_init($url);
curl_setopt_array($c, [
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 120,
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
CURLOPT_HTTPHEADER => $requestHeaders,
CURLOPT_HEADERFUNCTION => function($c, $line) use ($sentStatus, $isHeadRequest) {
if (! $sentStatus && ($sentStatus = curl_getinfo($c, CURLINFO_HTTP_CODE)) ) {
http_response_code($sentStatus);
}
// In redirection, this function is called for every subrequest.
// Only output headers if the most recent HTTP response is not a redirect,
// or in response to a HEAD request that's not following redirects.
if ($sentStatus &&
($isHeadRequest || ($sentStatus != 301 && $sentStatus != 302)) &&
! headers_sent()
) { header(trim($line)); }
return strlen($line);
}
]);
if ($isHeadRequest) {
curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'HEAD');
} else {
curl_setopt_array($c, [
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 5,
CURLOPT_REDIR_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS,
]);
}
curl_exec($c); // streams output to stdout
curl_close($c);
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.