What are PHP streams? Let's talk about the streams you have been using but have ignored

What are PHP streams? Let's talk about the streams you have been using but have ignored

definition

The purpose of streams is to use a unified way to handle operations such as files, networks, and data compression that share the same set of functions and usage. In simple terms, a stream is a resource object with streaming behavior. Therefore, a stream can be read and written linearly, and may also use the fseek() function to locate any position in the stream-PHP manual.

Let's simplify it for easy understanding. The purpose of a stream is to transfer data between a source and a destination. The source and destination can be files, command-line processes, network connections, ZIP or TAR archives, temporary memory, standard input or output, or any other resource implemented through the PHP stream encapsulation protocol.

[[284469]]

Stream Encapsulation Protocol

There are different types of streaming data, and each type requires a unique protocol to read and write data. We call these protocols stream encapsulation protocols. The purpose of stream encapsulation protocols is to encapsulate the differences between different communication methods using a common interface. Each stream has a protocol and a destination. The format is as follows:

  1. < scheme > :// < target >  

Where <scheme> is the encapsulation protocol of the stream, <target> Is the source of the stream.

Example: Using HTTP Stream Encapsulation to communicate with the Flickr API

  1. <? php  
  2. $ json = file_get_contents ('http://api.flickr.com/services/feeds/photos_public.gne? format = json ');

The string argument to the file_get_contents() function is actually a stream identifier. The http protocol tells PHP to use the HTTP stream wrapper. In this argument, after http is the stream target. The stream target looks like a normal web URL because the HTTP stream wrapper specifies that. Other stream wrappers may not be like this. (A normal URL is actually a PHP stream wrapper identifier in disguise).

file://stream encapsulation protocol

We use file_get_contents(), fopen(), fwrite(), and fclose() to read and write to the file system, and because PHP uses file:// as the default stream wrapper, we rarely think of these functions as using PHP streams. We use PHP streams without even realizing it!

Example: Implicit use of file:// stream wrapper

  1. <? php  
  2. $ handle = fopen ('/etc/hosts', 'rb');
  3. while(feof($handle) !== true) {
  4. echo fgets($handle);
  5. }
  6. fclose($handle);

The following example does the same thing, but this time we explicitly specify the stream file:// stream encapsulation protocol in the stream identifier:

Example: Explicitly using the file:// stream wrapper protocol

  1. <? php  
  2. $ handle = fopen ('file:///etc/hosts', 'rb');
  3. while(feof($handle) !== true) {
  4. echo fgets($handle);
  5. }
  6. fclose($handle);

We usually omit the file:// wrapper since this is the default used by PHP.

php://stream encapsulation protocol

This stream encapsulation protocol is used to communicate with the standard input, standard output, and standard error file descriptors of the PHP script. We can use the file system functions provided by PHP to open, read, or write the following four streams:

  1. php://stdin

This is a read-only PHP stream where data comes from standard input. For example, a PHP script can use this stream to receive information passed to the script on the command line.

  1. php://stdout

The purpose of this PHP stream is to write data to the current output buffer. This stream can only be written, not read or addressed.

  1. php://memory

The purpose of this PHP stream is to read data from system memory or write data to system memory. The disadvantage of this PHP stream is that the available memory is limited, and it is safer to use the php://temp stream.

  1. php://temp

This PHP stream works similarly to php://memory, however, when there is no available memory, PHP will write the data to a temporary file.

Other stream encapsulation protocols

PHP and PHP extensions also provide many other stream encapsulation protocols, for example, for communicating with ZIP and TAR archives, FTP servers, data compression libraries, etc.

Flow context

Some PHP streams can accept a series of optional parameters, called stream context, which are used to customize the behavior of the stream. The stream context is created using the stream_context_create() function. The context object returned by this function can be passed to most file system and stream functions.

Example: Stream context (sending an HTTP POST request using the file_get_contents() function)

  1. <? php  
  2. $ requestBody = '{"username":"josh"}' ;
  3. $ context = stream_context_create (array(
  4. 'http' = > array(
  5. 'method' = > 'POST',
  6. 'header' = > "Content-Type: application/json; charset = utf -8;\r\n" .
  7. "Content-Length: " .mb_strlen($requestBody),
  8. 'content' = > $requestBody
  9. )
  10. ));
  11. $ response = file_get_contents ('https://my-api.com/users', false, $context);

Stream Filters

The real power of PHP lies in filtering, transforming, adding or removing data transmitted in the stream.

<<:  How do Internet giants achieve load balancing and high availability? You will understand after reading this article

>>:  Deepin Technology was invited to attend the 2019 Annual Meeting of Jiangxi Health Information and Health Medical Big Data Society and delivered an important speech

Recommend

edgeNAT Hong Kong VPS host simple test

We have shared edgeNAT several times in the tribe...

Eight use cases for NV overlay

Most IT organizations are under pressure to be mo...

What problems do HTTP/1, HTTP/2, and HTTP/3 solve?

What problems does each generation of HTTP solve?...

2020 is already halfway through, how far is 5G from a full-scale outbreak?

In 2019, we often heard the industry say that 201...

How big data empowers 5G value operation and maintenance

The background and significance of data empowerin...

The greater development of 5G lies in industrial applications

[[181724]] Some people say that 4G has changed ou...