【问题标题】:How to partially download a remote file with cURL?如何使用 cURL 部分下载远程文件?
【发布时间】:2011-01-03 05:25:20
【问题描述】:

是否可以使用 cURL 部分下载远程文件? 假设远程文件的实际文件大小为 1000 KB。 我怎样才能只下载它的前 500 KB?

【问题讨论】:

标签: php curl


【解决方案1】:

您还可以使用 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);

【讨论】:

  • +1 以获得好的答案。这是因为 $writefn() 在达到限制时返回 -1。当回调函数返回的不是传递给它的字节数(在 $chunk 中)时,curl 中止连接。
  • 优秀。当我在 Perl 中做同样的事情时,我不得不使用触发并检查文件大小的警报,因为没有更好的方法。非常hacky,但它有效。
  • 正是我所需要的。然而,值得一提的是定义“块”大小的 CURLOPT_BUFFERSIZE。因此,如果您的缓冲区非常大,那么块大小可能很容易包含完整的网页数据(我认为!)
  • -1,因为这会为其他范围(超过 0)提供无效结果。您的回答给人的印象是这适用于所有范围以及不支持部分内容的服务器。然而,当服务器忽略范围并发送整个内容时,只有第一个范围得到处理,与设置的范围无关。要获得正确的结果,必须忽略范围偏移之前的第一个字节。
  • @JamesCameron 你能详细说明一下吗?那有什么办法呢?
【解决方案2】:

获取文档的前 100 个字节:

curl -r 0-99 http://www.get.this

来自手册

确保你有一个现代的卷发

【讨论】:

  • 你是对的,但是我发现它并不总是可靠的,并且取决于服务器而不是 curl 本身。在行为不端的情况下,curl 会继续下载。
  • 当我使用不同的范围(例如 100-200)时无法下载。我得到“卷曲错误(18)”。这能解决吗?
【解决方案3】:

感谢 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 // 或对结果执行任何操作。

【讨论】:

    【解决方案4】:

    这可能是您的解决方案(将前 500KB 下载到 output.txt

    curl -r 0-511999 http://www.yourwebsite.com > output.txt
    
    • 511999500^1024-1

    【讨论】:

    • 我敢打赌是500*1024-1
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2011-11-19
    • 2020-02-29
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    相关资源
    最近更新 更多