【发布时间】:2011-01-03 05:25:20
【问题描述】:
是否可以使用 cURL 部分下载远程文件? 假设远程文件的实际文件大小为 1000 KB。 我怎样才能只下载它的前 500 KB?
【问题讨论】:
是否可以使用 cURL 部分下载远程文件? 假设远程文件的实际文件大小为 1000 KB。 我怎样才能只下载它的前 500 KB?
【问题讨论】:
您还可以使用 php-curl 扩展名设置范围标头参数。
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.spiegel.de/');
curl_setopt($ch, CURLOPT_RANGE, '0-500');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
但如前所述,如果服务器不遵守此标头但发送整个文件 curl 将下载所有文件。例如。 http://www.php.net 忽略标题。但是您可以(另外)设置写入函数回调并在收到更多数据时中止请求,例如
// php 5.3+ only
// use function writefn($ch, $chunk) { ... } for earlier versions
$writefn = function($ch, $chunk) {
static $data='';
static $limit = 500; // 500 bytes, it's only a test
$len = strlen($data) + strlen($chunk);
if ($len >= $limit ) {
$data .= substr($chunk, 0, $limit-strlen($data));
echo strlen($data) , ' ', $data;
return -1;
}
$data .= $chunk;
return strlen($chunk);
};
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.php.net/');
curl_setopt($ch, CURLOPT_RANGE, '0-500');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn);
$result = curl_exec($ch);
curl_close($ch);
【讨论】:
获取文档的前 100 个字节:
curl -r 0-99 http://www.get.this
来自手册
确保你有一个现代的卷发
【讨论】:
感谢 VolkerK 的出色解决方案。但是我需要将此代码用作函数,所以这就是我想出的。我希望它对其他人有用。主要区别是 use ($limit, &$datadump) 因此可以传递限制,并使用通过引用变量 $datadump 能够将其作为结果返回。我还添加了 CURLOPT_USERAGENT,因为有些网站在没有用户代理标头的情况下不允许访问。
查看http://php.net/manual/en/functions.anonymous.php
function curl_get_contents_partial($url, $limit) {
$writefn = function($ch, $chunk) use ($limit, &$datadump) {
static $data = '';
$len = strlen($data) + strlen($chunk);
if ($len >= $limit) {
$data .= substr($chunk, 0, $limit - strlen($data));
$datadump = $data;
return -1;
}
$data .= $chunk;
return strlen($chunk);
};
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
//curl_setopt($ch, CURLOPT_RANGE, '0-1000'); //not honored by many sites, maybe just remove it altogether.
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn);
$data = curl_exec($ch);
curl_close($ch);
return $datadump;
}
用法:
$page = curl_get_contents_partial('http://some.webpage.com', 1000); //阅读 前 1000 个字节
echo $page // 或对结果执行任何操作。
【讨论】:
这可能是您的解决方案(将前 500KB 下载到 output.txt)
curl -r 0-511999 http://www.yourwebsite.com > output.txt
511999 是500^1024-1
【讨论】:
500*1024-1。