【问题标题】:PHP cURL - Get remaining time of downloadPHP cURL - 获取剩余下载时间
【发布时间】:2015-02-04 16:14:55
【问题描述】:

我让我的用户使用远程上传从(例如)他们自己的服务器而不是本地上传直接在我的服务器上下载内容。为此,我正在使用 cURL。现在我想获得 cURL 完成下载所需的剩余时间(比特率也可以)。

有没有一种方法可以返回 cURL 通过 PHP curl 模块完成下载所需的剩余时间,或者我是否需要运行命令行界面并以某种方式将输出放入文件中,然后从那里读取(因为使用shell_exec()exec()时PHP会阻止执行?

我已经获得了预期下载的字节数以及已经下载了多少 curl。这是迄今为止的相关代码:

function write_progress($ch, $original_size, $current_size, $os_up, $cs_up) {
    global $anitube, $cache;

    $cache->write("webupload-progress-".$anitube->input['inputname'], serialize(array("total" => $original_size, "loaded" => $current_size)));
}

ini_set('max_execution_time', '0');
$handle_file = "/tmp/file-".$anitube->generate(20).".tmp";
if(DIRECTORY_SEPARATOR == "\\") {
    $handle_file = IN_DIR.$handle_file;
}

$file = fopen($handle_file, "w+");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, urldecode(trim($anitube->input['filename'])));
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, "write_progress");
if($anitube->users['ip'] == "127.0.0.1") {
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
}

$data = curl_exec($ch);
if(curl_errno($ch) > 0) {
    file_put_contents(IN_DIR."/logs/curl_errors.txt", date("d.m.Y H:i:s")." - Errno: ".curl_errno($ch)." - Error: ".curl_error($ch)." -  cURL 4: ".print_r(curl_getinfo($ch), true)."\n", FILE_APPEND);
    die(json_encode(array("success" => 0, "response" => $language->word('remote_file_not_available'))));
} elseif(curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200) {
    file_put_contents(IN_DIR."/logs/curl_errors.txt", date("d.m.Y H:i:s")." - Error: Connection denied - HTTP-Response-Code: ".curl_getinfo($ch, CURLINFO_HTTP_CODE)." -  cURL 4: ".print_r(curl_getinfo($ch), true)."\n", FILE_APPEND);
    die(json_encode(array("success" => 0, "response" => $language->word('remote_file_not_available'))));
}

curl_close($ch);
fclose($file);

【问题讨论】:

  • 也许curl_getinfo() 有你需要的东西?它提供下载速度和REQUEST_SIZE 等信息。也许你可以计算出估计的剩余时间?我不确定这是否是正确的方法。
  • @aug curl_getinfo() 返回有关上次传输的信息。这意味着在转移完成后,这不是我需要的。我以前也看到过,但是因为完成后太晚了,所以我把它扔回去了。

标签: php curl


【解决方案1】:

PHP 的 cURL 库似乎没有提供估计的剩余时间,但是使用 CURLOPT_PROGRESSFUNCTION 回调函数从 PHP 计算这个是相当简单的。我在下面创建了一个工作示例。请注意,如果启用 GZIP 压缩,输出可能会延迟到整个请求完成。

示例:

<?php

header( 'Content-Type: text/plain' );

//A helper function to flush output buffers.
function flush_all() {
    while ( ob_get_level() ) {
        ob_end_flush();
    }
    flush();
}

$download_start_time = null;
function write_progress( $ch, $original_size, $current_size, $os_up, $cs_up ) {
    global $download_start_time;
    //Get the current time.
    $now = microtime( true );
    //Remember the start time.
    if ( ! $download_start_time ) {
        $download_start_time = $now;
    }
    //Check if the download size is available yet.
    if ( $original_size ) {
        //Compute time spent transfering.
        $transfer_time = $now - $download_start_time;
        //Compute percent already downloaded.
        $transfer_percentage = $current_size / $original_size;
        //Compute estimated transfer time.
        $estimated_tranfer_time = $transfer_time / $transfer_percentage;
        //Compute estimated time remaining.
        $estimated_time_remaining = $estimated_tranfer_time - $transfer_time;
        //Output the remaining time.
        var_dump( $estimated_time_remaining );
        flush_all();
    }
}

//Example usage.
$file = fopen( 'tmp.bin', "w+");
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://ftp.mozilla.org/pub/mozilla.org/firefox/releases/35.0.1/win32/en-US/Firefox%20Setup%2035.0.1.exe' );
curl_setopt( $ch, CURLOPT_FILE, $file );
curl_setopt( $ch, CURLOPT_NOPROGRESS, false );
curl_setopt( $ch, CURLOPT_PROGRESSFUNCTION, 'write_progress' );
$data = curl_exec( $ch );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    • 2011-03-27
    相关资源
    最近更新 更多