Cara menggunakan data stream php

AWS SDK for PHP memberikan akses ke API untuk bucket Amazon S3 dan operasi objek. SDK memberi Anda opsi untuk menggunakan API tingkat rendah atau menggunakan abstraksi tingkat tinggi.

SDK tersedia di AWS SDK for PHP, yang juga memiliki instruksi untuk memasang dan memulai SDK.

Pengaturan untuk menggunakan AWS SDK for PHP tergantung pada lingkungan Anda dan bagaimana Anda ingin menjalankan aplikasi Anda. Untuk menyiapkan lingkungan Anda untuk menjalankan contoh dalam dokumentasi ini, lihat Panduan Memulai AWS SDK for PHP.

Topik

  • Tingkat AWS SDK for PHP
  • Menjalankan Contoh PHP
  • Sumber Daya Terkait

Tingkat AWS SDK for PHP

AWS SDK for PHP memberi Anda opsi untuk menggunakan API tingkat tinggi atau tingkat rendah.

API Tingkat Rendah

API tingkat rendah mirip dengan operasi REST Amazon S3 yang mendasarinya, termasuk operasi buat, perbarui, dan hapus pada bucket dan objek. API tingkat rendah memberikan kendali yang lebih besar atas operasi ini. Misalnya, Anda dapat membuat batch permintaan dan menjalankannya secara paralel. Atau, saat menggunakan API unggahan multibagian, Anda dapat mengelola bagian objek secara terpisah. Perhatikan bahwa panggilan API tingkat rendah ini mengembalikan hasil yang mencakup semua perincian respons Amazon S3. Untuk informasi lebih lanjut tentang API unggahan multibagian, lihat Unggah dan menyalin objek menggunakan unggahan multipart API.

Abstraksi Tingkat Tinggi

Abstraksi tingkat tinggi dimaksudkan untuk menyederhanakan kasus penggunaan umum. Misalnya, untuk mengunggah objek besar menggunakan API tingkat rendah, Anda memanggil Aws\S3\S3Client::createMultipartUpload(), memanggil metode Aws\S3\S3Client::uploadPart() untuk mengunggah bagian objek, lalu memanggil metode Aws\S3\S3Client::completeMultipartUpload() untuk menyelesaikan pengunggahan. Anda dapat menggunakan objek Aws\S3\\MultipartUploader yang levelnya yang lebih tinggi yang menyederhanakan pembuatan pengunggahan multibagian.

Sebagai contoh lain, saat menghitung objek dalam bucket, Anda dapat menggunakan fitur iterator dari AWS SDK for PHP untuk mengembalikan semua kunci objek, terlepas dari berapa banyak objek yang telah Anda simpan di dalam bucket. Jika Anda menggunakan API tingkat rendah, respons akan mengembalikan maksimal 1.000 kunci. Jika bucket berisi lebih dari 1.000 objek, hasilnya akan dipotong dan Anda harus mengelola respons dan memeriksa pemangkasan.

Menjalankan Contoh PHP

Untuk menyiapkan dan menggunakan contoh Amazon S3 untuk versi 3 dari AWS SDK for PHP, lihat Pemasangan di Panduan Developer AWS SDK for PHP.

  • AWS SDK for PHPuntuk Amazon S3

  • AWSDokumentasi SDK for PHP Documentation

  • AWSAPI SDK for PHP untuk Amazon S3

  • AWSContoh kode SDK for PHP versi 3

Input/output streams

The CLI SAPI defines a few constants for I/O streams to make programming for the command line a bit easier.

CLI specific Constants
ConstantDescription
STDIN

An already opened stream to stdin. This saves opening it with

<?php
$stdin 
fopen('php://stdin''r');
?>

If you want to read single line from stdin, you can use

<?php
$line 
trim(fgets(STDIN)); // reads one line from STDIN
fscanf(STDIN"%d\n"$number); // reads number from STDIN
?>

STDOUT

An already opened stream to stdout. This saves opening it with

<?php
$stdout 
fopen('php://stdout''w');
?>

STDERR

An already opened stream to stderr. This saves opening it with

<?php
$stderr 
fopen('php://stderr''w');
?>

Given the above, you don't need to open e.g. a stream for stderr yourself but simply use the constant instead of the stream resource:

php -r 'fwrite(STDERR, "stderr\n");'

You do not need to explicitly close these streams, as they are closed automatically by PHP when your script ends.

Note:

These constants are not available if reading the PHP script from stdin.

Aurelien Marchand

11 years ago

Please remember in multi-process applications (which are best suited under CLI), that I/O operations often will BLOCK signals from being processed.

For instance, if you have a parent waiting on fread(STDIN), it won't handle SIGCHLD, even if you defined a signal handler for it, until after the call to fread has returned.

Your solution in this case is to wait on stream_select() to find out whether reading will block. Waiting on stream_select(), critically, does NOT BLOCK signals from being processed.

Aurelien

phil_php at zieaon dot com

4 years ago

The command line interface data in STDIN is not made available until return is pressed.
By adding "readline_callback_handler_install('', function(){});" before reading STDIN for the first time single key presses can be captured.

Note: This only seems to work under Linux CLI and will not work in Apache or Windows CLI.

This cam be used to obscure a password or used with 'stream_select' to make a non blocking keyboard monitor.

<?php// Demo WITHOUT readline_callback_handler_install('', function(){});
   
$resSTDIN=fopen("php://stdin","r");
    echo(
"Type 'x'. Then press return.");
   
$strChar = stream_get_contents($resSTDIN, 1);

    echo(

"\nYou typed: ".$strChar."\n\n");
   
fclose($resSTDIN);// Demo WITH readline_callback_handler_install('', function(){});
// This line removes the wait for <CR> on STDIN
   
readline_callback_handler_install('', function(){});$resSTDIN=fopen("php://stdin","r");
    echo(
"We have now run: readline_callback_handler_install('', function(){});\n");
    echo(
"Press the 'y' key");
   
$strChar = stream_get_contents($resSTDIN, 1);
    echo(
"\nYou pressed: ".$strChar."\nBut did not have to press <cr>\n");
   
fclose($resSTDIN);
   
readline_callback_handler_remove ();
    echo(
"\nGoodbye\n")
?>

It also hides text from the CLI so can be used for things like. password obscurification.
eg

<?php
    readline_callback_handler_install
('', function(){});
    echo(
"Enter password followed by return. (Do not use a real one!)\n");
    echo(
"Password: ");
   
$strObscured='';
    while(
true)
    {
   
$strChar = stream_get_contents(STDIN, 1);
    if(
$strChar===chr(10))
    {
        break;
    }
   
$strObscured.=$strChar;
    echo(
"*");
    }
    echo(
"\n");
    echo(
"You entered: ".$strObscured."\n");
?>

phil_php at zieaon dot com

2 years ago

Under Linux CLI - STDIN, STDOUT and STDERR can be closed and reconnected to a different php stream such as a file, pipe or even a UDP socket_stream. (I use this technique to send the output/errors of my long running background scripts to a file so I can debug if something goes wrong.)

For example: (The below creates/appends file "/tmp/php_stdout.txt")
<?php
// This only works under CLI in Linux
// Note: Until we have closed it STDOUT will NOT be prefixed with a $

// Get the path to the current console for STDOUT so we can reconnect later!

$strOldSTDOUT=(posix_ttyname(STDOUT)); 

    echo(

"This will go to the current console\r\n");
// Close the STDOUT resource     
   
fclose(STDOUT);    // Reopen $STDOUT as a file     Note: All further $STDOUT usage will be prefixed with a $
   
$STDOUT=fopen("/tmp/php_stdout.txt","a");  /
    echo(
"This should append the file /tmp/php_stdout.txt\r\n");
// Close stdout again so we can reconnect the console. Note: We are still using
   
fclose($STDOUT);    // Use the path to the console we got earlier   
   
$STDOUT=fopen($strOldSTDOUT,"r+");
    echo(
"And we are back on the console\r\n");?>

ecrist at secure-computing dot net

11 years ago

The following code shows how to test for input on STDIN.  In this case, we were looking for CSV data, so we use fgetcsv to read STDIN, if it creates an array, we assume CVS input on STDIN, if no array was created, we assume there's no input from STDIN, and look, later, to an argument with a CSV file name.

Note, without the stream_set_blocking() call, fgetcsv() hangs on STDIN, awaiting input from the user, which isn't useful as we're looking for a piped file. If it isn't here already, it isn't going to be.

<?php
stream_set_blocking
(STDIN, 0);
$csv_ar = fgetcsv(STDIN);
if (
is_array($csv_ar)){
  print
"CVS on STDIN\n";
} else {
  print
"Look to ARGV for CSV file name.\n";
}
?>

ayon at hyurl dot com

5 years ago

I find a BUG with the constant STDIN, I don't know if it si the Enter/Return key that make this proprem, when I use trim(fgets(STDIN)), that doesn't trim anything, when I detect the length of fgets(STDIN), in windows, it is 2 characters longer than what I input, in Linux, it makes 1. I tried to trim(fgets(STDIN), ' \r\n'), but it still does not work.
So I have to substr the input manually, it seems like this way:
<?php
$STDIN
= trim(substr(fgets(STDIN), 0, (PHP_OS == 'WINNT' ? 2 : 1)));
?>
then I get what I want really.