How to use Stream (stream) in PHP _php tutorial

Source: Internet
Author: User
Tags spl
In Java, flow is a very important concept.

The concept of stream is derived from the concept of piping (pipe) in Unix. In Unix, a pipeline is an uninterrupted stream of bytes used to communicate between programs or processes, or to read and write peripherals, external files, and so on. According to the direction of the flow can be divided into the input and output streams, but also on the periphery of other flows, such as buffer flow, so that more flow processing methods can be obtained.

The stream in PHP and the stream in Java are actually the same concept, just a little bit simpler. Since PHP is primarily used for web development, the concept of "flow" is less mentioned. If you have a Java base, it's easier to understand the flow in PHP. In fact, many of the advanced features in PHP, such as SPL, anomalies, filters, and so on are referenced in the implementation of Java, in the concept and principle of expatiating.

For example, here is the use of a PHP SPL standard library (traversing directories to find fixed-condition files):

Copy the Code code as follows:
Class Recursivefilefilteriterator extends Filteriterator
{
The extension that satisfies the condition
Protected $ext = array (' jpg ', ' gif ');

/**
* Provide $path and generate the corresponding directory iterator
*/
Public function __construct ($path)
{
Parent::__construct (New Recursiveiteratoriterator (New Recursivedirectoryiterator ($path)));
}

/**
* Check if the file name extension meets the criteria
*/
Public Function Accept ()
{
$item = $this->getinneriterator ();
if ($item->isfile () && In_array (PathInfo ($item->getfilename (), pathinfo_extension), $this->ext))
{
return TRUE;
}
}
}

Instantiation of
foreach (New Recursivefilefilteriterator (' D:/history ') as $item)
{
Echo $item. Php_eol;
}

Java also has code for its expatiating:

Copy the Code code as follows:
public class Directorycontents
{
public static void Main (string[] args) throws IOException
{
File F = new file ("."); Current directory

FilenameFilter textfilter = new FilenameFilter ()
{
Public Boolean Accept (File dir, String name)
{
String lowercasename = Name.tolowercase ();
if (Lowercasename.endswith (". txt"))
{
return true;
}
Else
{
return false;
}
}
};

file[] files = f.listfiles (TextFilter);

for (File file:files)
{
if (File.isdirectory ())
{
System.out.print ("directory:");
}
Else
{
System.out.print ("File:");
}

System.out.println (File.getcanonicalpath ());
}
}
}

This example shows that PHP and Java have the same concept in many aspects, that mastering one language can be very helpful in understanding another language, and this example also helps us to refer to the filter flow-filter below. In fact, it is also a design pattern embodiment.

We can start with a few examples to understand the use of stream series functions.

Here is an example of using a socket to fetch data:

Copy the Code code as follows:
$post _ =array (
' Author ' = ' Gonn ',
' Mail ' = ' gonn@nowamagic.net ',
' url ' = ' http://www.nowamagic.net/',
' Text ' = ' Welcome to the Concise modern Magic ');

$data =http_build_query ($post _);
$fp = Fsockopen ("Nowamagic.net", $errno, $errstr, 5);

$out = "POST http://nowamagic.net/news/1/comment http/1.1\r\n";
$out. = "host:typecho.org\r\n";
$out. = "user-agent:mozilla/5.0 (Windows; U Windows NT 6.1; ZH-CN; rv:1.9.2.13) gecko/20101203 firefox/3.6.13 "." \ r \ n ";
$out. = "content-type:application/x-www-form-urlencoded\r\n";
$out. = "phpsessid=082b0cc33cc7e6df1f87502c456c3eb0\r\n";
$out. = "Content-length:". Strlen ($data). "\ r \ n";
$out. = "connection:close\r\n\r\n";
$out. = $data. " \r\n\r\n ";

Fwrite ($fp, $out);
while (!feof ($FP))
{
Echo fgets ($FP, 1280);
}

Fclose ($FP);

We can also use the Stream_socket implementation, which is very simple, just need to open the socket code to replace the following:

Copy the Code code as follows:
$fp = Stream_socket_client ("tcp://nowamagic.net:80", $errno, $errstr, 3);

Let's look at a stream example:

The File_get_contents function is commonly used to read the contents of a file, but this function can also be used to crawl remote URLs, playing a similar role as curl.

Copy the Code code as follows:
$opts = Array (
' HTTP ' =>array (
' Method ' = ' POST ',
' Header ' = ' content-type:application/x-www-form-urlencoded\r\n '.
"Content-length:". Strlen ($data). "\ r \ n",
' Content ' = $data)
);

$context = Stream_context_create ($opts);
file_get_contents (' Http://www.jb51.net/news ', false, $context);

Note the third argument, $context, the HTTP stream context, can be understood as a pipe set on the File_get_contents function. Similarly, we can also create an FTP stream, a socket stream, and set it in the corresponding function.

More about Stream_context_create, you can refer to: PHP function Completion: stream_context_create () simulation post/get.

The function of the two stream series mentioned above is a wrapper-like flow that acts on the input and output stream of some kind of protocol. Such use and concept, in fact, and Java in the flow is not a big difference, such as Java often have such a way of writing:

Copy the Code code as follows:
New DataOutputStream (New Bufferedoutputstream (New FileOutputStream (FileName)));

A laminar layer is nested in another laminar flow, and PHP is similar to the wonderful.

Let's look at the effect of a filter flow:

Copy the Code code as follows:
$fp = fopen (' c:/test.txt ', ' w+ ');

/* Put the ROT13 filter on the write stream */
Stream_filter_append ($fp, "string.rot13", stream_filter_write);

/* The data written is processed by the ROT13 filter */
Fwrite ($FP, "This is a test\n");
Rewind ($FP);

/* Read the written data, the original nature is the processed characters */
Fpassthru ($FP);
Fclose ($FP);

OUTPUT:GUVF VF N GRFG

In the above example, if we set the type of the filter to Stream_filter_all, that is, at the same time on the read-write stream, the read-write data will be processed by the ROT13 filter, and the data we read is consistent with the original data being written.

You may be surprised by the stream_filter_append of the "string.rot13" variable, which is actually a filter built into PHP.

Use the following method to print out the PHP built-in flow:

Copy the Code code as follows:
Streamlist = Stream_get_filters ();
Print_r ($streamlist);

Output:

Copy the Code code as follows:
Array
(
[0] = convert.iconv.*
[1] = mcrypt.*
[2] = mdecrypt.*
[3] = string.rot13
[4] = String.ToUpper
[5] = String.ToLower
[6] = String.strip_tags
[7] = convert.*
[8] = Consumed
[9] = Dechunk
[Ten] = zlib.*
[One] = bzip2.*
)

Naturally, we will think of defining our own filters, which is not difficult:

Copy the Code code as follows:
Class Md5_filter extends Php_user_filter
{
function filter ($in, $out, & $consumed, $closing)
{
while ($bucket = stream_bucket_make_writeable ($in))
{
$bucket->data = MD5 ($bucket->data);
$consumed + = $bucket->datalen;
Stream_bucket_append ($out, $bucket);
}

Data processing is successful and can be read by other pipelines
return psfs_pass_on;
}
}
Stream_filter_register ("String.md5", "Md5_filter");

Note: The filter name can be arbitrarily taken.

You can then use the "String.md5" as our custom filter.

The way the filter is written seems to be a bit confusing, in fact we just need to look at the structure of the Php_user_filter class and the built-in approach to understanding it.

Filter flow is the most suitable to do is the file format conversion, including compression, codec, in addition to these "partial gate" usage, the filter stream is more useful in debugging and logging functions, such as in the socket development, register a filter stream to log records. For example, the following:

Copy the Code code as follows:
Class Md5_filter extends Php_user_filter
{
Public Function filter ($in, $out, & $consumed, $closing)
{
$data = "";
while ($bucket = stream_bucket_make_writeable ($in))
{
$bucket->data = MD5 ($bucket->data);
$consumed + = $bucket->datalen;
Stream_bucket_append ($out, $bucket);
}

Call_user_func ($this->params, $data);
return psfs_pass_on;
}
}

$callback = function ($data)
{
File_put_contents ("C:\log.txt", Date ("Y-m-d h:i"). " \ r \ n ");
};

This filter can not only process the input stream, but also callback a function for logging.

You can use this:

Copy the Code code as follows:
Stream_filter_prepend ($fp, "String.md5", Stream_filter_write, $callback);

There is also a very important stream in the Stream series function in PHP, which is the wrapper class flow streamwrapper. Using wrapper flow allows different types of protocols to manipulate data using the same interface.

http://www.bkjia.com/PHPjc/745822.html www.bkjia.com true http://www.bkjia.com/PHPjc/745822.html techarticle in Java, flow is a very important concept. The concept of stream is derived from the concept of piping (pipe) in Unix. In Unix, a pipeline is an uninterrupted stream of bytes used to implement a program or process ...

  • 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.