Skip to content

Instantly share code, notes, and snippets.

@grzegorz-rozycki
Created July 11, 2017 09:57
Show Gist options
  • Save grzegorz-rozycki/f9e8d07f07f0a130620a9604e1828bc8 to your computer and use it in GitHub Desktop.
Save grzegorz-rozycki/f9e8d07f07f0a130620a9604e1828bc8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
define('URL1', 'http://php.net/manual/en/ref.stream.php');
define('URL2', 'http://php.net/manual/en/function.stream-filter-append.php');
function stream_make_notification_handler(string $stream_id) {
return function ($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) use ($stream_id) {
echo "STREAM: {$stream_id}\t";
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);
/* Ignore */
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";
};
}
try {
$ctx1 = stream_context_create();
$ctx2 = stream_context_create();
stream_context_set_params($ctx1, [ 'notification' => stream_make_notification_handler('1') ]);
stream_context_set_params($ctx2, [ 'notification' => stream_make_notification_handler('2') ]);
$s1 = fopen(URL1, 'r', false, $ctx1);
$s2 = fopen(URL2, 'r', false, $ctx2);
stream_set_blocking($s1, false);
stream_set_blocking($s2, false);
while (!(feof($s1) && feof($s2))) {
stream_select($read = [ $s1, $s2 ], $write = [], $except = null, 3);
stream_get_contents($s1);
stream_get_contents($s2);
}
} finally {
fclose($s1);
fclose($s2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment