Streams is a powerful tool that PHP provides, and we often inadvertently use it, and if good usage will greatly increase PHP productivity. After harnessing the power of streams, the application will be elevated to a new level.
The following is a description of streams in the PHP manual:
Copy Code code as follows:
Streams is introduced in PHP version 4.3.0, and it is used in the operation of file, network, data compression and other class files, and provides a set of common function interfaces for these file operations. In short, a stream is a resource object that has a streaming behavior. In other words, we can read and write the stream in a linear way. And you can use Fseek () to jump to any position within the stream.
Each streams object has a wrapper class that can add code to handle special protocols and encodings in the wrapper. There are some common wrapper classes already built into PHP, and we can create and register custom wrapper classes. We can even modify and enhance the wrapper class using the existing context and filter.
Stream Basic Knowledge
The Stream can be referenced by way of <scheme>://<target>. Where <scheme> is the name of the wrapper class the contents of the,<target> are specified by the syntax of the wrapper class, and the syntax of the different wrapper classes varies.
The default wrapper class for PHP is file://, which means that when we access the filesystem, we actually use a stream. There are two ways in which we can read the contents of a file, ReadFile ('/path/to/somefile.txt ') or ReadFile (' File:///path/to/somefile.txt '), which is equivalent. If you are using ReadFile (' http://google.com/'), then PHP chooses the HTTP stream wrapper class to operate.
As mentioned above, PHP offers a number of built-in package-turn classes, protocol, and filter. You can query the wrapper classes supported by this computer, as described in the following ways:
Copy Code code as follows:
<?php
Print_r (Stream_get_transports ());
Print_r (Stream_get_wrappers ());
Print_r (Stream_get_filters ());
The output on my machine is:
Copy Code code as follows:
Array
(
[0] => TCP
[1] => UDP
[2] => Unix
[3] => UDG
[4] => SSL
[5] => SSLv3
[6] => Sslv2
[7] => TLS
)
Array
(
[0] => HTTPS
[1] => FTPs
[2] => Compress.zlib
[3] => COMPRESS.BZIP2
[4] => PHP
[5] => file
[6] => Glob
[7] => data
[8] => http
[9] => FTP
[Ten] => zip
[One] => Phar
)
Array
(
[0] => zlib.*
[1] => bzip2.*
[2] => convert.iconv.*
[3] => string.rot13
[4] => String.ToUpper
[5] => String.ToLower
[6] => String.strip_tags
[7] => convert.*
[8] => consumed
[9] => Dechunk
[Ten] => mcrypt.*
[One] => mdecrypt.*
)
It offers a lot of functionality, does it look good?
In addition to the built-in stream, we can also write more third-party streams for Amazon S3, MS Excel, Google Storage, Dropbox, and even Twitter.
php://streams Packing Class
In PHP, a wrapper class is built in this language to handle I/O stream. Can be divided into several categories, based on Php://stdin,php://stdout, and Php://stderr, which map to the default I/O resources respectively. PHP also provides php://input, a wrapper class that accesses the raw body in a POST request in a read-only manner. This is a very useful feature, especially when dealing with remote services that embed data loads into a POST request.
Below we use the Curl tool to do a simple test:
Copy Code code as follows:
Curl-d "Hello World"-D "Foo=bar&name=john" http://localhost/dev/streams/php_input.php
The test results for using Print_r ($_post) in PHP scripts are as follows:
Copy Code code as follows:
Array
(
[foo] => Bar
[Name] => John
)
We note that the first item of data cannot be accessed in the $_post array. But if we use ReadFile (' Php://input '), the result is different:
Copy Code code as follows:
Hello World&foo=bar&name=john
PHP 5.1 also adds php://memory and Php://tempstream, which are used to read and write temporary data. As implied in the wrapper class naming, the data is stored in memory or temporary files in the underlying system.
Php://filter is a meta wrapper class that is used to increase the filter function for the stream. When the stream is opened using ReadFile () or file_get_contents ()/stream_get_contents (), the filter is enabled. Here is an example:
Copy Code code as follows:
<?php
Write encoded data
File_put_contents ("Php://filter/write=string.rot13/resource=file:///path/to/somefile.txt", "Hello World");
Read Data and Encode/decode
ReadFile ("php://filter/read=string.toupper|string.rot13/resource=http://www.google.com");
In the first example, a filter was used to encode data that was saved to disk, and in two cases, two cascaded filter was used to read data from the remote URL. Using filter can bring a powerful function to your application.
Stream context
A context is a set of stream-related parameters or options that you can use to modify or enhance the behavior of the wrapper class. For example, using the context to modify an HTTP wrapper is a common usage scenario. This allows us to do some simple network operations without using the Curl tool. Here is an example:
Copy Code code as follows:
<?php
$opts = Array (
' HTTP ' =>array (
' Method ' => ' POST ',
' Header ' => ' Auth:secretauthtokenrn '.
"Content-type:application/x-www-form-urlencodedrn".
"Content-length:". strlen ("Hello World"),
' Content ' => ' Hello world '
)
);
$default = Stream_context_get_default ($opts);
ReadFile (' http://localhost/dev/streams/php_input.php ');
First you define a options array, which is a two-bit array that you can use to access the arguments in the form of $array[' wrapper ' [' option_name ']. (Note that the options for the context in each wrapper class are different). The Stream_context_get_default () is then invoked to set these Option,stream_context_get_default () and return the default context as a result. After the setup is complete, the next call to ReadFile () will apply the context you just set to crawl the content.
In the above example, the content is embedded in the body of the request so that the remote script can use Php://input to read the content. At the same time, we can use Apache_request_headers () to get the header of the request, as follows:
Copy Code code as follows:
Array
(
[Host] => localhost
[Auth] => Secretauthtoken
[Content-type] => application/x-www-form-urlencoded
[Content-length] => 11
)
In the example above, the parameters of the default context are modified, but we can also create a new context for alternate use.
Copy Code code as follows:
<?php
$alternative = Stream_context_create ($other _opts);
ReadFile (' http://localhost/dev/streams/php_input.php ', false, $alternative);
Streams conclusion
How do we manage the power of the stream in the real world? What are the practical benefits of using stream to our programs? As described earlier, the stream has an abstraction of all file system-related functions, so the first scenario I thought of was using the wrapper class of the virtual file system to access the services provided by the PAAs provider, such as access to Heroku or APPFOG, which actually didn't have a real filesystem. Using the stream, you can migrate it to the cloud as long as you make a slight modification to our application. Next – In my next article – I'll explain how to write a custom wrapper class to work with special file formats and encoding formats.