note 82844 added to features.remote-files
| From: | mail at 3v1n0 dot net | Date: | Tue, 29 Apr 2008 01:18:04 +0000 |
| Subject: | note 82844 added to features.remote-files | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138887@lists.php.net to get a copy of this message | ||
I've changed the function below to support the 4xx errors and the 30x redirects... This is a
partial implementation yet but it's sufficient for the normal usage.
I've made a recursive implementation (if a 30x redirect is found), but it could be easily
reverted to an iterative way (simple put something like while (!empty $url) at the beginning, and
set the $url to an empty string if no 3xx/4xx status are found).
<?
function http_get($url, $range = 0)
{
$url_stuff = parse_url($url);
$port = isset($url_stuff['port']) ? $url_stuff['port'] : 80;
$fp = @fsockopen($url_stuff['host'], $port);
if (!$fp)
return false;
$query = 'GET
'.$url_stuff['path'].'?'.$url_stuff['query']."
HTTP/1.1\r\n";
$query .= 'Host: '.$url_stuff['host']."\r\n";
$query .= 'Connection: close'."\r\n";
$query .= 'Cache-Control: no'."\r\n";
$query .= 'Accept-Ranges: bytes'."\r\n";
if ($range != 0)
$query .= 'Range: bytes='.$range.'-'."\r\n"; // -500
//$query .= 'Referer: http:/...'."\r\n";
//$query .= 'User-Agent: myphp'."\r\n";
$query .= "\r\n";
fwrite($fp, $query);
$chunksize = 1*(1024*1024);
$headersfound = false;
while (!feof($fp) && !$headersfound) {
$buffer .= @fread($fp, 1);
if (preg_match('/HTTP\/[0-9]\.[0-9][ ]+([0-9]{3}).*\r\n/', $buffer, $matches)) {
$headers['HTTP'] = $matches[1];
$buffer = '';
} else if (preg_match('/([^:][A-Za-z_-]+):[ ]+(.*)\r\n/', $buffer, $matches)) {
$headers[$matches[1]] = $matches[2];
$buffer = '';
} else if (preg_match('/^\r\n/', $buffer)) {
$headersfound = true;
$buffer = '';
}
if (strlen($buffer) >= $chunksize)
return false;
}
if (preg_match('/4[0-9]{2}/', $headers['HTTP']))
return false;
else if (preg_match('/3[0-9]{2}/', $headers['HTTP']) &&
!empty($headers['Location'])) {
$url = $headers['Location'];
return http_get($url, $range);
}
while (!feof($fp) && $headersfound) {
$buffer = @fread($fp, $chunksize);
echo $buffer;
ob_flush();
flush();
}
$status = fclose($fp);
return $status;
}
?>
----
Server IP: 208.69.120.35
Probable Submitter: 151.49.63.141
----
Manual Page -- http://www.php.net/manual/en/features.remote-files.php
Edit -- https://master.php.net/note/edit/82844
Del: integrated -- https://master.php.net/note/delete/82844/integrated
Del: useless -- https://master.php.net/note/delete/82844/useless
Del: bad code -- https://master.php.net/note/delete/82844/bad+code
Del: spam -- https://master.php.net/note/delete/82844/spam
Del: non-english -- https://master.php.net/note/delete/82844/non-english
Del: in docs -- https://master.php.net/note/delete/82844/in+docs
Del: other reasons-- https://master.php.net/note/delete/82844
Reject -- https://master.php.net/note/reject/82844
Search -- https://master.php.net/manage/user-notes.php