【问题标题】:Capture Progress From Command Line从命令行捕获进度
【发布时间】:2010-11-10 16:02:53
【问题描述】:

% Total % Received % Xferd 平均速度 时间 时间 时间 当前 下载上传总花费剩余速度 100 12.4M 100 12.4M 0 0 4489k 0 0:00:02 0:00:02 --:--:-- 4653k

以上是下载文件时从命令行输出的 CURL。我已经像这样使用 PHP 捕获了这一点,但是我无法弄清楚如何使用 pre_match 来提取完成的百分比。

$handle = popen('curl -o '.VIDEOPATH.$fileName.'.flv '.$url, 'rb');

while(!feof($handle))
{
    $progress = fread($handle, 8192);
    //I don't even know what I was attempting here
    $pattern = '/(?<Total>[0-9]{1,3}\.[0-9]{1,2})% of (?<Total>.+) at/';
    //divide received by total somehow, then times 100
    if(preg_match_all($pattern, $progress, $matches)){
    fwrite($fh, $matches[0][0]."\r\n");
    }

} 

我该怎么做?请注意,我不知道我在用上面的 preg_match_all 做什么!

谢谢

更新

感谢 ylebre 的帮助。到目前为止我有这个。

$handle = popen('curl -o '.VIDEOPATH.$fileName.'.flv '.$url.' 2>&1', 'rb');//make sure its saved to videos

while(!feof($handle))
{

    $line = fgets($handle, 4096); // Get a line from the input handle
    echo '<br>Line'.$line.'<br>';
    $line = preg_replace("/s+/", " ", $line); // replace the double spaces with one
    $fields = explode(" ", $line); // split the input on spaces into fields array
    echo '<br>Fields: '.$fields[0];
    fwrite($fh, $fields[0]); // write a part of the fields array to the output file

} 

我得到这个输出到浏览器:


行 % Total % Received % Xferd 平均速度 时间 时间 时间 当前

字段: Line Dload Upload Total 左转速度

字段: 行 0 1340k 0 4014 0 0 27342 0 0:00:50 --:--:-- 0:00:50 27342 41 1340k 41 552k 0 0 849k 0 0:00:01 --:--:-- 0 :00:01 1088k 100 1340k 100 1340k 0 0 1445k 0 --:--:-- --:--:-- --:--:-- 1711k

字段: 线


如何只提取百分比部分?也许 CURL 可以自己做到这一点 - 嗯会问这个问题。

【问题讨论】:

  • 嗯,我刚刚注意到百分比是第二行的第一件事,我怎么得到?

标签: php command-line curl


【解决方案1】:

显示的进度可能是在同一位置更新信息,因此如果您知道自己正在解析什么,这将有所帮助。

我建议的下一步是输入一行,并尝试让正则表达式处理它。

如果我正确读取输出,您也可以在空格处拆分字符串。如果您首先将所有双空格替换为一个。之后您可以使用explode() 来获取一个包含值的数组,您可以通过print_r 查看里面的内容。

这可能是这样的:

$line = fgets($handle, 4096); // Get a line from the input handle
$line = preg_replace("/s+/", " ", $line); // replace the double spaces with one
$fields = explode(" ", $line); // split the input on spaces into fields array
fwrite($fh, $fields[0]); // write a part of the fields array to the output file

只要字段中的顺序保持不变,您的结果数组就应该为您提供一致的结果。

希望这会有所帮助!

【讨论】:

  • 啊!这看起来像是我能理解的。 :) 谢谢你的解释。我试了一下,我设法收到了上面的输出(更新的问题) - 它似乎得到了两条线,但没有得到百分比部分。无论如何,我可以进一步编辑吗?谢谢。
  • 如果数组如我所愿填充,您应该能够访问 $fields[1] 中的百分比字段。
【解决方案2】:

如果您可以访问 PHP 5.3,则可以使用 CURL_PROGRESSFUNCTION 选项,这会产生更优雅的解决方案(无解析输出)。这是一个如何使用它的示例:

function callback($download_size, $downloaded, $upload_size, $uploaded)
{
  $percent=$downloaded/$download_size;
  // Do something with $percent
}

$ch = curl_init('http://www.example.com');

// Turn off the default progress function
curl_setopt($ch, CURLOPT_NOPROGRESS, false);

// Set up the callback
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback');

// You'll want to tweak the buffer size.  Too small could affect performance.  Too large and you don't get many progress callbacks.
curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);

$data = curl_exec($ch);

【讨论】:

    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 2010-12-20
    • 2017-04-28
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 2012-11-22
    相关资源
    最近更新 更多