php教程

超轻量级php框架startmvc

浅谈PHP中Stream(流)

更新时间:2020-03-04 19:13:27 作者:startmvc
流(stream)的概念源于UNIX中管道(pipe)的概念。在UNIX中,管道是一条不间断的字节流,用来实

流(stream)的概念源于UNIX中管道(pipe)的概念。在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备、外部文件等。根据流的方向又可以分为输入流和输出流,同时可以在其外围再套上其它流,比如缓冲流,这样就可以得到更多流处理方法。

PHP里的流和Java里的流实际上是同一个概念,只是简单了一点。由于PHP主要用于Web开发,所以“流”这块的概念被提到的较少。如果有Java基础,对于PHP里的流就更容易理解了。其实PHP里的许多高级特性,比如SPL,异常,过滤器等都参考了Java的实现,在理念和原理上同出一辙。

比如下面是一段PHP SPL标准库的用法(遍历目录,查找固定条件的文件):


class RecursiveFileFilterIterator extends FilterIterator
{
 // 满足条件的扩展名
 protected $ext = array('jpg','gif');
 /**
 * 提供 $path 并生成对应的目录迭代器
 */
 public function __construct($path)
 {
 parent::__construct(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)));
 }
 /**
 * 检查文件扩展名是否满足条件
 */
 public function accept()
 {
 $item = $this->getInnerIterator();
 if ($item->isFile() && in_array(pathinfo($item->getFilename(), PATHINFO_EXTENSION), $this->ext))
 {
 return TRUE;
 }
 }
}
// 实例化
foreach (new RecursiveFileFilterIterator('D:/history') as $item)
{
 echo $item . PHP_EOL;
}

Java里也有和其同出一辙的代码:


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());
 }
 }
}

举这个例子,一方面是说明PHP和Java在很多方面的概念是一样的,掌握一种语言对理解另外一门语言会有很大的帮助;另一方面,这个例子也有助于我们下面要提到的过滤器流-filter。其实也是一种设计模式的体现。

我们可以通过几个例子先来了解stream系列函数的使用。

下面是一个使用socket来抓取数据的例子:


$post_ =array (
 'author' => 'Gonn',
 'mail'=>'gonn@nowamagic.net',
 'url'=>'http://www.nowamagic.net/',
 'text'=>'欢迎访问简明现代魔法');
$data=http_build_query($post_);
$fp = fsockopen("nowamagic.net", 80, $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);

我们也可以用stream_socket 实现,这很简单,只需要打开socket的代码换成下面的即可:


$fp = stream_socket_client("tcp://nowamagic.net:80", $errno, $errstr, 3);

再来看一个stream的例子:

file_get_contents函数一般常用来读取文件内容,但这个函数也可以用来抓取远程url,起到和curl类似的作用。


$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://nowamagic.net/news/1/comment', false, $context);

注意第三个参数,$context,即HTTP流上下文,可以理解为套在file_get_contents函数上的一根管道。同理,我们还可以创建FTP流,socket流,并把其套在对应的函数在。

PHP Stream