PHP cURL - 获取剩余的下载时间 [英] PHP cURL - Get remaining time of download

查看:374
本文介绍了PHP cURL - 获取剩余的下载时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要让我的用户使用远程上传功能直接从我的服务器上下载内容(例如)自己的服务器,而不是本地上传。为此,我使用cURL。现在我想得到余下的时间cURL需要完成下载(比特率也会很好)。



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



以及已下载的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(dmY H:i:s)。 - Errno: .curl_errno($ ch)。 - 错误:.curl_error($ ch)。 - cURL 4:.print_r(curl_getinfo($ ch),true)\\\
,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(dmY H:i:s)。错误:连接被拒绝 - HTTP响应代码:.curl_getinfo($ ch,CURLINFO_HTTP_CODE)。 - cURL 4:.print_r(curl_getinfo($ ch),true)\\\
,FILE_APPEND);
die(json_encode(array(success=> 0,response=> $ language-> word('remote_file_not_available')))));
}

curl_close($ ch);
fclose($ file); PHP的cURL库似乎没有提供估计的时间。 剩下的,但是使用 CURLOPT_PROGRESSFUNCTION 回调函数从PHP计算它是相当微不足道。我在下面创建了一个工作示例。请注意,如果启用了GZIP压缩,则输出可能会延迟到整个请求完成。



示例:



 <?php 

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

//帮助函数刷新输出缓冲区。
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;
//获取当前时间。
$ now = microtime(true);
//记住开始时间。
if(!$ download_start_time){
$ download_start_time = $ now;
}
//检查下载大小是否可用。
if($ original_size){
//计算转移所花费的时间。
$ transfer_time = $ now - $ download_start_time;
//计算已下载的百分比。
$ transfer_percentage = $ current_size / $ original_size;
//计算估计的传输时间。
$ estimated_tranfer_time = $ transfer_time / $ transfer_percentage;
//计算剩余估计时间。
$ estimated_time_remaining = $ estimated_tranfer_time - $ transfer_time;
//输出剩余时间。
var_dump($ estimated_time_remaining);
flush_all();
}
}

//用法示例。
$ 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。可执行程序' );
curl_setopt($ ch,CURLOPT_FILE,$ file);
curl_setopt($ ch,CURLOPT_NOPROGRESS,false);
curl_setopt($ ch,CURLOPT_PROGRESSFUNCTION,'write_progress');
$ data = curl_exec($ ch);


I am offering my users to use remote-upload to download the content directly on my server from (for example) their own server instead of local uploads. For that I'm using cURL. And now I want to get the remaining time cURL needs to complete the download (bitrate would be fine too).

Is there a way I can return the remaining time cURL needs to finish the download via the PHP curl module or do I need to run the command-line interface and somehow put the output in a file and then read from there (since PHP blocks execution when using shell_exec() or exec())?

I'm already getting the bytes expected to download and how many curl already downloaded. This is the associated code as far:

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);

解决方案

PHP's cURL library does not appear to provide the estimated time remaining, but it is fairly trivial to calculate this from PHP using the CURLOPT_PROGRESSFUNCTION callback function. I've created a working example below. Note that if GZIP compression is enabled, the output will probably be delayed until the entire request is complete.

Example:

<?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 );

这篇关于PHP cURL - 获取剩余的下载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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