理解 PHP 中的 Streams

Streams 是PHP提供的一个强有力的工具,我们常常在不经意会使用到它,如果善加利用将大大提高PHP的生产力。 驾驭Streams的强大力量后,应用程序将提升到一个新的高度。

  下面是PHP手册中对Streams的一段描述:

Streams 是在PHP 4.3.0版本被引入的,它被用于统一文件、网络、数据压缩等类文件的操作方式,为这些类文件操作提供了一组通用的函数接口。简而言之,一个stream就是一个具有流式行为的资源对象。也就是说,我们可以用线性的方式来对stream进行读取和写入。并且可以用使用fseek()来跳转到stream内的任意位置。

  每个Streams对象都有一个包装类,在包装中可以添加处理特殊协议和编码的相关代码。PHP中已经内置了一些常用的包装类,我们也可以创建和注册自定义的包装类。我们甚至能够使用现有的context和filter对包装类进行修改和增强。

 Stream 基础知识

  Stream 可以通过<scheme>://<target>方式来引用。其中<scheme>是包装类的名字,<target>中的内容是由包装类的语法指定,不同的包装类的语法会有所不同。

  PHP默认的包装类是file://,也就是说我们在访问文件系统的时候,其实就是在使用一个stream。我们可以通过下面两种方式来读取文件中的内容,readfile(‘/path/to/somefile.txt‘)或者readfile(‘file:///path/to/somefile.txt‘),这两种方式是等效的。如果你是使用readfile(‘http://google.com/‘),那么PHP会选取HTTP stream包装类来进行操作。

  正如上文所述,PHP提供了不少内建的包转类,protocol以及filter。 按照下文所述的方式,可以查询到本机所支持的包装类:


1

2

3

4

<?php

print_r(stream_get_transports());

print_r(stream_get_wrappers());

print_r(stream_get_filters());

  在我机器上的输出结果为:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

Array

(

    [0] => tcp

    [1] => udp

    [2] => <span id="12_nwp" style="width: auto; height: auto; float: none;"><a id="12_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=a7fa9b6f96d65b10&k=unix&k0=unix&kdi0=0&luki=1&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=105bd6966f9bfaa7&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5420%2Ehtml&urlid=0" target="_blank" mpid="12" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">unix</span></a></span>

    [3] => udg

    [4] => <span id="13_nwp" style="width: auto; height: auto; float: none;"><a id="13_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=a7fa9b6f96d65b10&k=ssl&k0=ssl&kdi0=0&luki=2&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=105bd6966f9bfaa7&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5420%2Ehtml&urlid=0" target="_blank" mpid="13" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">ssl</span></a></span>

    [5] => sslv3

    [6] => sslv2

    [7] => tls

)

Array

(

    [0] => https

    [1] => ftps

    [2] => compress.zlib

    [3] => compress.bzip2

    [4] => php

    [5] => file

    [6] => glob

    [7] => <span id="14_nwp" style="width: auto; height: auto; float: none;"><a id="14_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=a7fa9b6f96d65b10&k=data&k0=data&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=105bd6966f9bfaa7&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5420%2Ehtml&urlid=0" target="_blank" mpid="14" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">data</span></a></span>

    [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.*

)

  提供的功能非常多,看上去还不错吧?

  除了上述内建的Stream,我们还可以为 Amazon S3MS ExcelGoogle StorageDropbox 甚至Twitter编写更多的第三方的Stream。

 php:// 包装类

  PHP中内建了本语言用于处理I/O stream的包装类。可以分为几类,基础的有php://stdin,php://stdout, 以及php://stderr,这3个stream分别映射到默认 的I/O资源。同时PHP还提供了php://input,通过这个包装类可以使用只读的方式访问POST请求中的raw body。 这是一项非常有用的功能,特别是在处理那些将数据负载嵌入到POST请求中的远程服务时。

  下面我们使用cURL工具来做一个简单的测试:


1

curl -d "Hello World" -d "foo=bar&name=John" <a href="http://localhost/dev/streams/php_input.php">http://localhost/dev/streams/php_input.php</a>

  在PHP脚本中使用print_r($_POST)的测试结果如下所示:


1

2

3

4

5

Array

(

    [foo] => bar

    [name] => John

)

  我们注意$_POST array中是无法访问到第一项数据的。但是如果我们使用readfile(‘php://input‘),结果就不同了:


1

Hello World&foo=<span id="11_nwp" style="width: auto; height: auto; float: none;"><a id="11_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=a7fa9b6f96d65b10&k=bar&k0=bar&kdi0=0&luki=3&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=105bd6966f9bfaa7&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5420%2Ehtml&urlid=0" target="_blank" mpid="11" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">bar</span></a></span>&name=John

  PHP 5.1又增加了php://memory和php://tempstream这两个包转类,用于读写临时数据。正如包装类命名中所暗示的,这些数据被存储在底层系统中的内存或者临时文件中。

php://filter是一个元包装类,用于为stream增加filter功能。在使用readfile()或者file_get_contents()/stream_get_contents()打开stream时,filter将被使能。下面是一个例子:


1

2

3

4

5

6

<?<span id="9_nwp" style="width: auto; height: auto; float: none;"><a id="9_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=a7fa9b6f96d65b10&k=php&k0=php&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=105bd6966f9bfaa7&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5420%2Ehtml&urlid=0" target="_blank" mpid="9" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span>

// Write encoded <span id="10_nwp" style="width: auto; height: auto; float: none;"><a id="10_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=a7fa9b6f96d65b10&k=data&k0=data&kdi0=0&luki=8&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=105bd6966f9bfaa7&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5420%2Ehtml&urlid=0" target="_blank" mpid="10" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">data</span></a></span>

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");

  在第一个例子中使用了一个filter来对保存到磁盘中的数据进行编码处理,在二个例子中,使用两个级联的filter来从远端的URL读取数据。使用filter能为你的应用带来极为强大的功能。

 Stream上下文

  context是一组stream相关的参数或选项,使用context可以修改或增强包装类的行为。例如使用context来修改HTTP包装器是一个常用到的使用场景。 这样我们就可以不使用cURL工具,就能完成一些简单的网络操作。下面是一个例子:


1

2

3

4

5

6

7

8

9

10

11

12

<?<span id="6_nwp" style="width: auto; height: auto; float: none;"><a id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=a7fa9b6f96d65b10&k=php&k0=php&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=105bd6966f9bfaa7&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5420%2Ehtml&urlid=0" target="_blank" mpid="6" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span>

$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);

  首先要定义一个options array,这是个二位数组,可以通过$array[‘wrapper‘][‘option_name‘]的形式来访问其中的参数。(注意每个包装类中context的options是不同的)。然后调用stream_context_get_default()来设置这些option,stream_context_get_default()同时还会将默认的context作为结果返回回来。设置完成后,接下来调用readfile(),就会应用刚才设置好的context来抓取内容。

  在上面的例子中,内容被嵌入到request的body中,这样远端的脚本就可以使用php://input来读取这些内容。同时,我们还能使用apache_request_headers()来获取request的header,如下所示:


1

2

3

4

5

6

7

Array

(

    [Host] => localhost

    [Auth] => SecretAuthToken

    [Content-type] => application/x-www-form-urlencoded

    [Content-length] => 11

)

  在上面的例子中是修改默认context的参数,当然我们也可以创建一个新的context,进行交替使用。


1

2

3

<?<span id="4_nwp" style="width: auto; height: auto; float: none;"><a id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=a7fa9b6f96d65b10&k=php&k0=php&kdi0=0&luki=10&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=105bd6966f9bfaa7&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F5420%2Ehtml&urlid=0" target="_blank" mpid="4" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span>

$alternative = stream_context_create($other_opts);

readfile(http://localhost/dev/streams/php_input.php, false, $alternative);

 结论

  我们怎样在现实世界中驾驭stream的强大力量呢?使用stream能为我们的程序带来什么现实的好处? 正如前文介绍的那样,stream对所有文件系统相关的功能进行了抽象,所以我第一个想到的应用场景是使用虚拟文件系统的包装类来访问PaaS供应商提供的服务,比如说访问HeroKu或者AppFog,它们实际上都没有真正文件系统。 使用stream只要对我们的应用程序稍作修改,就可以将其移植到云端。 接下来--在我的下一篇文章中--我将介绍如何编写自定义的包装类以实现对特殊文件格式和编码格式的操作。

  原文地址:http://www.sitepoint.com/%EF%BB%BFunderstanding-streams-in-php/

时间: 2024-08-09 10:43:34

理解 PHP 中的 Streams的相关文章

转:理解 PHP 中的 Streams

本文转自:开源中国社区 [http://www.oschina.net]本文标题:理解 PHP 中的 Streams 本文地址:http://www.oschina.net/translate/understanding-streams-in-php参与翻译: YiHunter 英文原文:Understanding Streams in PHP Streams 是PHP提供的一个强有力的工具,我们常常在不经意会使用到它,如果善加利用将大大提高PHP的生产力. 驾驭Streams的强大力量后,应用

深入理解CSS中的层叠上下文和层叠顺序(转)

by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=5115 零.世间的道理都是想通的 在这个世界上,凡事都有个先后顺序,凡物都有个论资排辈.比方说食堂排队打饭,对吧,讲求先到先得,总不可能一拥而上.再比如说话语权,老婆的话永远是对的,领导的话永远是对的. 在CSS届,也是如此.只是,一般情况下,大家歌舞升平,看不出什么差异,即所谓的众生平等.但是,当发生冲突发生纠葛的时

理解Linux中的load Averges

一.什么是load average? linux系统中的Load对当前CPU工作量的度量 (WikiPedia: the system load is a measure of the amount of work that a computer system is doing).也有简单的说是进程队列的长度. Load Average 就是一段时间 (1 分钟.5分钟.15分钟) 内平均 Load . 我们可以通过系统命令"w"查看当前load average情况 [[email p

深入理解CSS中的margin

1.css margin可以改变容器的尺寸 元素尺寸 可视尺寸--标准盒子模型中盒子的宽度是不包括margin值的,clientWidth 占据尺寸--包括margin的宽度 outWidth不在标准之中,jquery中有相对应的方法 margin与可视尺寸 1.1使用那个与没有设定width/height的普通block水平元素 2.2只适用于水平方向尺寸 <body style="background-color:#1a2b3c"> <div style=&quo

深入理解JavaScript中创建对象模式的演变(原型)

创建对象的模式多种多样,但是各种模式又有怎样的利弊呢?有没有一种最为完美的模式呢?下面我将就以下几个方面来分析创建对象的几种模式: Object构造函数和对象字面量方法 工厂模式 自定义构造函数模式 原型模式 组合使用自定义构造函数模式和原型模式 动态原型模式.寄生构造函数模式.稳妥构造函数模式 第一部分:Object构造函数和对象字面量方法 我之前在博文<javascript中对象字面量的理解>中讲到过这两种方法,如何大家不熟悉,可以点进去看一看回顾一下.它们的优点是用来创建单个的对象非常方

深入理解JavaScript中的属性和特性

深入理解JavaScript中的属性和特性? JavaScript中属性和特性是完全不同的两个概念,这里我将根据自己所学,来深入理解JavaScript中的属性和特性. 主要内容如下: 理解JavaScript中理解对象的本质.理解对象与类的关系.对象与引用类型的关系 对象属性如何进行分类 属性中特性的理解 第一部分:理解JavaScript中理解对象的本质.理解对象与类的关系.对象与引用类型的关系 对象的本质:ECMA-262把对象定义为:无序属性的集合,其属性可以包含基本值.对象或者函数.即

深入理解CSS中的层叠上下文和层叠顺序

零.世间的道理都是想通的 在这个世界上,凡事都有个先后顺序,凡物都有个论资排辈.比方说食堂排队打饭,对吧,讲求先到先得,总不可能一拥而上.再比如说话语权,老婆的话永远是对的,领导的话永远是对的. 在CSS届,也是如此.只是,一般情况下,大家歌舞升平,看不出什么差异,即所谓的众生平等.但是,当发生冲突发生纠葛的时候,显然,是不可能做到完全等同的,先后顺序,身份差异就显现出来了.例如,杰克和罗斯,只能一人浮在木板上,此时,出现了冲突,结果大家都知道的.那对于CSS世界中的元素而言,所谓的“冲突”指什

【干货理解】理解javascript中实现MVC的原理

理解javascript中的MVC MVC模式是软件工程中一种软件架构模式,一般把软件模式分为三部分,模型(Model)+视图(View)+控制器(Controller); 模型:模型用于封装与应用程序的业务逻辑相关的数据以及对数据处理的方法.模型有对数据直接访问的权利.模型不依赖 "视图" 和 "控制器", 也就是说 模型它不关心页面如何显示及如何被操作. 视图:视图层最主要的是监听模型层上的数据改变,并且实时的更新html页面.当然也包括一些事件的注册或者aja

storm源码之理解Storm中Worker、Executor、Task关系【转】

[原]storm源码之理解Storm中Worker.Executor.Task关系 Storm在集群上运行一个Topology时,主要通过以下3个实体来完成Topology的执行工作:1. Worker(进程)2. Executor(线程)3. Task 下图简要描述了这3者之间的关系:                                                    1个worker进程执行的是1个topology的子集(注:不会出现1个worker为多个topology服