Streams in PHP

Source: Internet
Author: User
Tags readfile
This article mainly introduces the Streams in PHP in detail. This article introduces the basic knowledge of Stream, php: Streams packaging class, Stream context, and so on, if you need it, you can refer to Streams, which is a powerful tool provided by PHP. we often use it inadvertently. if you use it well, it will greatly increase the productivity of PHP. With the power of Streams, applications will be elevated to a new level.

The following is a description of Streams in the PHP manual:

The code is as follows:


Streams was introduced in PHP 4.3.0. it is used to operate files in a unified way, such as files, networks, and data compression, provides a set of common function interfaces for these file operations. In short, a stream is a resource object with streaming behaviors. That is to say, we can use a linear method to read and write streams. You can also use fseek () to jump to any position in the stream.

Each Streams object has a packaging class. you can add code related to special protocols and codes to the packaging. PHP has built-in some common packaging classes. we can also create and register custom packaging classes. We can even use the existing context and filter to modify and enhance the packaging class.

Basic Stream knowledge

Stream can use :// . Where Is the name of the packaging class, The content in is specified by the syntax of the packaging class, and the syntax of different packaging classes will be different.

The default PHP packaging class is file: //. that is to say, when we access the file system, we actually use a stream. We can read the content in the file in the following two ways: readfile ('/path/to/somefile.txt') or readfile ('file: /// path/to/somefile.txt '). The two methods are equivalent. If you use readfile ('http: // google.com/'), PHP selects the http stream packaging class for operations.

As mentioned above, PHP provides many built-in conversion classes, protocols and filters. You can query the packages supported by the local machine as described below:

The code is as follows:


<? Php
Print_r (stream_get_transports ());
Print_r (stream_get_wrappers ());
Print_r (stream_get_filters ());

The output result on my machine is:

The code is 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
[10] => zip
[11] => 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
[10] => mcrypt .*
[11] => mdecrypt .*
)

Many features are provided. does it look good?

In addition to the built-in Stream above, we can also write more third-party streams for Amazon S3, MS Excel, Google Storage, Dropbox and even Twitter.

Php: // Streams packaging class

PHP has built-in packaging classes for processing I/O stream. Php: // stdin, php: // stdout, and php: // stderr, these three streams are mapped to the default I/O resources respectively. PHP also provides php: // input, which can be used to access the raw body in the POST request in read-only mode. This is a very useful feature, especially when processing remote services that embed data loads into POST requests.

Here we will use the cURL tool for a simple test:

The code is as follows:


Curl-d "Hello World"-d "foo = bar & name = John" http: // localhost/dev/streams/php_input.php


The test results of using print_r ($ _ POST) in a PHP script are as follows:

The code is as follows:


Array
(
[Foo] => bar
[Name] => John
)


We note that $ _ POST array cannot access the first item of data. However, if we use readfile ('php: // input'), the results will be different:

The code is as follows:


Hello World & foo = bar & name = John


PHP 5.1 adds the php: // memory and php: // tempstream conversion classes to read and write temporary data. As the packaging class name implies, the data is stored in the memory or temporary files of the underlying system.

Php: // filter is a meta-packaging class used to add the filter function for stream. When you use readfile () or file_get_contents ()/stream_get_contents () to open stream, the filter will be enabled. The following is an example:

The code is 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 stored on the disk. in the second example, two cascade filters are used to read data from the remote URL. Using filters can provide extremely powerful functions for your applications.

Stream context

Context is a set of stream-related parameters or options. you can use context to modify or enhance the behavior of the packaging class. For example, using context to modify the HTTP wrapper is a common application scenario. In this way, you can complete some simple network operations without using the cURL tool. The following is an example:

The code is 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, define an options array, which is a two-dimensional array. you can access the parameters in the form of $ array ['wrapper'] ['option _ name. (Note that the options of context in each packaging class are different ). Call stream_context_get_default () to set these options. stream_context_get_default () will return the default context as the result. After the settings are complete, call readfile () to apply the context you just set to capture the content.

In the above example, the content is embedded into the request body, so that remote scripts can use php: // input to read the content. At the same time, we can also use apache_request_headers () to obtain the request header, as shown below:

The code is as follows:


Array
(
[Host] => localhost
[Auth] => SecretAuthToken
[Content-type] => application/x-www-form-urlencoded
[Content-length] => 11
)

In the above example, we modify the default context parameter. of course, we can also create a new context for alternate use.

The code is as follows:


<? Php
$ Alternative = stream_context_create ($ other_opts );
Readfile ('http: // localhost/dev/streams/php_input.php ', false, $ alternative );

Streams conclusion

How can we control the power of stream in the real world? What are the practical benefits of using stream for our programs? As described above, stream abstracts all file system-related functions, so the first application scenario I thought of was to use the packaging class of the virtual file system to access the services provided by the PaaS vendor. for example, to access HeroKu or AppFog, they do not actually have a real file system. Stream can be transplanted to the cloud as long as our application is slightly modified. Next-in my next article-I will introduce how to write custom packaging classes to perform operations on special file formats and encoding formats.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.