使用cURL限制下载带宽 [英] Limiting Bandwidth of Download with cURL

查看:158
本文介绍了使用cURL限制下载带宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试用PHP限制带宽。我无法使用PHP限制下载速度。

I have been trying to limit the bandwidth with PHP. I can't get the download rate to be limited with PHP.

请问您在这里提供帮助吗?

Can you please help here?

function total_filesize($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "$url");
    curl_setopt($ch, CURLINFO_SPEED_DOWNLOAD,12); //ITS NOT WORKING! 
    curl_setopt($ch, CURLOPT_USERAGENT,
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) ".
            "Gecko/20071127 Firefox/2.0.0.11");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_NOBODY, true);


    $chStore = curl_exec($ch);
    $chError = curl_error($ch);
    $chInfo = curl_getinfo($ch);
    curl_close($ch);
    return $size = $chInfo['download_content_length'];
}

function __define_url($url) {
    $basename = basename($url);
    Define('filename',$basename);
    $define_file_size = total_filesize($url);
    Define('filesizes',$define_file_size);
}

function _download_file($url_file) { 
    __define_url($url_file);

    // $range = "50000-60000";
    $filesize = filesizes;
    $file = filename; 
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$file.'"'); 
    header('Content-Transfer-Encoding: binary');
    header("Content-Length: $filesize");
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"$url_file");
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    //  curl_setopt($ch, CURLOPT_RANGE,$range);

    curl_exec($ch);
    curl_close($ch);
}
_download_file('http://rarlabs.com/rar/wrar393.exe'); 

推荐答案

CURLOPT_MAX_RECV_SPEED_LARGE 是您想要的选项。

CURLOPT_MAX_RECV_SPEED_LARGE is the option you want.

在curl 7.15.5中添加。自 PHP 5.4.0

Added in curl 7.15.5. Present in PHP/CURL since PHP 5.4.0

这篇关于使用cURL限制下载带宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆