Streams is a powerful tool that PHP provides, and we often inadvertently use it, and if good use will greatly improve PHP productivity. When you harness the power of streams, your application will ascend to a new height.
The following is a description of streams in the PHP manual:
Copy the Code code as follows:
Streams was introduced in PHP 4.3.0, which is used to manipulate the file, network, data compression and other class files, providing a common set of function interfaces for these class file operations. In short, a stream is a resource object with streaming behavior. In other words, we can read and write the stream in a linear fashion. And you can use Fseek () to jump to any location within the stream.
Each streams object has a wrapper class that can add related code that handles special protocols and encodings in the wrapper. There are some commonly used wrapper classes built into PHP, and we can create and register custom wrapper classes. We can even use the existing context and filter to modify and enhance the wrapper class.
Stream Basics
Stream can be referenced by:// way. It is the name of the wrapper class, the contents of which are specified by the syntax of the wrapper class, and the syntax of the different wrapper classes will vary.
The default wrapper class for PHP is file://, which means that when we access the file system, we are actually using a stream. We can read the contents of the file in the following two ways, ReadFile ('/path/to/somefile.txt ') or ReadFile (' File:///path/to/somefile.txt '), both of which are equivalent. If you are using ReadFile (' http://google.com/'), then PHP will choose the HTTP stream wrapper class to operate.
As mentioned above, PHP provides a number of built-in package-to-class, protocol, and filter. You can query the wrapper classes supported by this machine as described below:
Copy the 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 the 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.*
)
There are a lot of features that look good, right?
In addition to the above 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, the wrapper class for processing I/O stream is built into this language. Can be divided into several categories, based on Php://stdin,php://stdout, and Php://stderr, which are 3 streams mapped to the default I/O resources. PHP also provides php://input, through which the wrapper class can access 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 payloads in a POST request.
Let's use the Curl tool to do a simple test:
Copy the 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 the Code code as follows:
Array
(
[Foo] = bar
[Name] = John
)
We note that the $_post array is inaccessible to the first item of data. But if we use ReadFile (' Php://input '), the result will be different:
Copy the Code code as follows:
Hello World&foo=bar&name=john
PHP 5.1 adds php://memory and Php://tempstream, which are used to read and write temporary data. As implied in the wrapper class naming, this data is stored in memory or temporary files in the underlying system.
Php://filter is a meta wrapper class that is used to add the filter function to the stream. Filter is enabled when the stream is opened using ReadFile () or file_get_contents ()/stream_get_contents (). Here is an example:
Copy the 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 is used to encode the data saved to disk, and in two cases, two cascading filter is used to read the data from the remote URL. Using filter can bring the most powerful features to your application.
Stream context
The 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 context to modify the 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 the 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 ');
The first thing to define is an options array, which is a two-bit array that can access the parameters in the form of $array[' wrapper ' [' option_name ']. (Note that the options for the context in each wrapper class are different). Then call Stream_context_get_default () 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 up to crawl the content.
In the example above, the content is embedded in the request body 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 the 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, and of course we can create a new context for alternate use.
Copy the 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 harness the power of stream in the real world? What practical benefits does using stream bring to our programs? As described earlier, stream abstracts all filesystem-related functionality, so my first scenario was to use the wrapper classes of the virtual file system to access the services provided by the PAAs vendors, such as access to Heroku or APPFOG, which actually did not have a real file system. With stream you can port it to the cloud as long as you make a slight change to our application. Next – In my next article – I'll show you how to write a custom wrapper class to implement operations on special file formats and encoding formats.