PHP Streams函数

PHP stream_notification_callback() 函数是一个可调用函数,由通知上下文参数使用,在事件期间调用。

注意:这不是一个真正的函数,只是该函数应该如何的原型。

语法

stream_notification_callback(notification_code, severity, 
                             message, message_code, 
                             bytes_transferred, bytes_max) 

参数

notification_code必填。 指定 STREAM_NOTIFY_* 通知常量之一。
sererity必填。 指定 STREAM_NOTIFY_SEVERITY_* 通知常量之一。
message必填。 如果事件有可用的描述性消息,则传递。
message_code必需。 如果事件有可用的描述性消息代码,则通过。该值的含义取决于所使用的特定包装器。
bytes_transferred必需。 如果适用,bytes_transferred 将被填充。
bytes_max必填。 如果适用,将填充bytes_max

返回值

无。

示例:stream_notification_callback() 示例

下面的示例显示了stream_notification_callback() 函数的用法。

<?php
function stream_notification_callback($notification_code, 
              $severity, $message, $message_code, 
              $bytes_transferred, $bytes_max) {

  switch($notification_code) {
    case STREAM_NOTIFY_RESOLVE:
    case STREAM_NOTIFY_AUTH_REQUIRED:
    case STREAM_NOTIFY_COMPLETED:
    case STREAM_NOTIFY_FAILURE:
    case STREAM_NOTIFY_AUTH_RESULT:
      var_dump($notification_code, $severity, $message, 
              $message_code, $bytes_transferred, $bytes_max);
      //忽略
      break;

    case STREAM_NOTIFY_REDIRECTED:
      echo "Being redirected to: $message";
      break;

    case STREAM_NOTIFY_CONNECT:
      echo "Connected...";
      break;

    case STREAM_NOTIFY_FILE_SIZE_IS:
      echo "Got the filesize: $bytes_max";
      break;

    case STREAM_NOTIFY_MIME_TYPE_IS:
      echo "Found the mime-type: $message";
      break;

    case STREAM_NOTIFY_PROGRESS:
      echo "Made some progress, downloaded $bytes_transferred so far";
      break;
}
echo "\n";
}

$ctx = stream_context_create();
stream_context_set_params($ctx, 
          array("notification" => "stream_notification_callback"));

file_get_contents("https://yxjc123.com/index", false, $ctx);
?> 

上述代码的输出将类似于:

Connected...
Got the filesize: 339
Found the mime-type: text/html; charset=iso-8859-1
Being redirected to: https://www.yxjc123.com/index.php
Connected...
Found the mime-type: text/html; charset=UTF-8
Made some progress, downloaded 0 so far
Made some progress, downloaded 0 so far
Made some progress, downloaded 5273 so far
Made some progress, downloaded 9657 so far
Made some progress, downloaded 9659 so far
Made some progress, downloaded 9665 so far
Made some progress, downloaded 13965 so far
Made some progress, downloaded 21207 so far
Made some progress, downloaded 21209 so far
Made some progress, downloaded 21884 so far 

示例:命令行下载客户端的简单进度条

考虑下面的示例在使用命令行下载客户端的简单进度条时使用此函数。

<?php
function usage($argv) {
    echo "Usage:\n";
    printf("\tphp %s <http://example.com/file> <localfile>\n", $argv[0]);
    exit(1);
}

function stream_notification_callback($notification_code, 
                      $severity, $message, $message_code, 
                      $bytes_transferred, $bytes_max) {
static $filesize = null;

switch($notification_code) {
  case STREAM_NOTIFY_RESOLVE:
  case STREAM_NOTIFY_AUTH_REQUIRED:
  case STREAM_NOTIFY_COMPLETED:
  case STREAM_NOTIFY_FAILURE:
  case STREAM_NOTIFY_AUTH_RESULT:
    //忽略
      break;

  case STREAM_NOTIFY_REDIRECTED:
    echo "Being redirected to: $message \n";
    break;

  case STREAM_NOTIFY_CONNECT:
    echo "Connected...\n";
    break;

  case STREAM_NOTIFY_FILE_SIZE_IS:
    $filesize = $bytes_max;
    echo "Filesize: $filesize \n";
    break;

  case STREAM_NOTIFY_MIME_TYPE_IS:
    echo "Mime-type: $message \n";
    break;

  case STREAM_NOTIFY_PROGRESS:
    if ($bytes_transferred > 0) {
      if (!isset($filesize)) {
        printf("\rUnknown filesize.. %2d kb done..", 
                           $bytes_transferred/1024);
      } else {
        $length = (int)(($bytes_transferred/$filesize)*100);
        printf("\r[%-100s] %d%% (%2d/%2d kb)", 
           str_repeat("=", $length). ">", 
           $length, 
           ($bytes_transferred/1024), 
           $filesize/1024);
      }
    }
    break;
  }
}

isset($argv[1], $argv[2]) or usage($argv);

$ctx = stream_context_create();
stream_context_set_params($ctx, 
       array("notification" => "stream_notification_callback"));

$fp = fopen($argv[1], "r", false, $ctx);
if (is_resource($fp) && file_put_contents($argv[2], $fp)) {
  echo "\nDone!\n";
  exit(0);
}

$err = error_get_last();
echo "\nError...\n", $err["message"], "\n";
exit(1);
?> 

使用以下命令执行上面的示例: php -n fetch.php http://no2.php.net/get/php-5-LATEST.tar.bz2/from/this/mirror php-latest .tar.bz2 也会输出类似的内容:

Connected...
Mime-type: text/html; charset=utf-8
Being redirected to: http://no2.php.net/distributions/php-5.2.5.tar.bz2
Connected...
Filesize: 7773024
Mime-type: application/octet-stream
[========================================>                                                           ] 40% (3076/7590 kb)