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:
- 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:
<?php
Print_r (Stream_get_transports ());
Print_r (Stream_get_wrappers ());
Print_r (Stream_get_filters ());
The output on my machine is:
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
[A] => 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://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:
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:
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:
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:
<?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:
<?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:
Array
(
[Host] => localhost
[Auth] => secretauthtoken
[Content-type] => application/ X-www-form-urlencoded
[content-length] =>
)
In the example above, the parameters of the default context are modified, but we can also create a new context for alternate use.
<?php
$alternative = stream_context_create ($other _opts);
ReadFile (' http://localhost/dev/streams/php_input.php ', false, $alternative);
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.