PHP - Stream扩展 - 学习/实践

1.应用场景

主要用于学PHP中的stream扩展,提供的功能实现,以及如何在项目使用。

2.学习/操作

1.文档阅读

PHP: 其它基本扩展 - Manual

PHP: Streams - Manual -- 推荐好好看看

2.整理输出

2.1 是什么

简介 ¶

Streams are the way of generalizing file, network, data compression, and other operations which share a common set of functions and uses. In its simplest definition, a stream is a resource object which exhibits streamable behavior. That is, it can be read from or written to in a linear fashion, and may be able to fseek() to an arbitrary location within the stream.

wrapper is additional code which tells the stream how to handle specific protocols/encodings. For example, the http wrapper knows how to translate a URL into an HTTP/1.0 request for a file on a remote server. There are many wrappers built into PHP by default (See 支持的协议和封装协议), and additional, custom wrappers may be added either within a PHP script using stream_wrapper_register(), or directly from an extension. Because any variety of wrapper may be added to PHP, there is no set limit on what can be done with them. To access the list of currently registered wrappers, use stream_get_wrappers().

A stream is referenced as: scheme://target

  • scheme(string) - The name of the wrapper to be used. Examples include: file, http, https, ftp, ftps, compress.zlib, compress.bz2, and php. See 支持的协议和封装协议 for a list of PHP built-in wrappers. If no wrapper is specified, the function default is used (typically file://).
  • target - Depends on the wrapper used. For filesystem related streams this is typically a path and filename of the desired file. For network related streams this is typically a hostname, often with a path appended. Again, see 支持的协议和封装协议 for a description of targets for built-in streams.

翻译:

简介 ¶

流是概括文件、网络、数据压缩 和 其他共享一组通用功能和用途的操作的方式。

在其最简单的定义中,流是表现出可流式行为的资源对象。也就是说,它可以以线性方式读取或写入,并且可以 fseek() 到流中的任意位置。

包装器是告诉流如何处理特定协议/编码附加代码。

例如,http 包装器知道如何将 URL 转换为对远程服务器上文件的 HTTP/1.0 请求。

默认情况下,PHP 中内置了许多包装器(请参阅支持的协议和封装协议),另外,可以使用 stream_wrapper_register 在 PHP 脚本中添加自定义包装器或者直接从扩展中添加自定义包装器。

因为任何种类的包装器都可以添加到 PHP 中,所以对它们可以做什么没有设定限制。

要访问当前注册的包装器列表,请使用 stream_get_wrappers()。

流被引用为:scheme://target

scheme(string) - 要使用的包装器的名称。

示例包括:file、http、https、ftp、ftps、compress.zlib、compress.bz2 和 php。

有关 PHP 内置包装器的列表,请参见支持的协议和封装。如果未指定包装器,则使用函数默认值(通常为 file://)。
目标 - 取决于使用的包装器。对于文件系统相关的流,这通常是所需文件的路径和文件名。对于网络相关的流,这通常是一个主机名,通常附加一个路径。同样,请参阅支持的协议和封装协议以了解内置流的目标描述。

2.2 为什么需要「应用场景」

为了让PHP功能更加强大,能够更加轻松方便地做更多事情。

这不是很多编程语言都想做的吗?!

2.3 什么时候出现「历史发展」

TBD

2.4 实践

1. 使用前知道

需求 ¶

构建此扩展不需要其他扩展。

安装

使用这些函数不需要安装,它们是 PHP 核心的一部分。

即可知,已被集成在PHP源码中,拿来即用即可。

2. 看下PHP当前注册的包装器列表

主要是先知道下,有哪些包装齐「实际上是想知道,支持哪些协议」

不过文档上说,「因为任何种类的包装器都可以添加到 PHP 中,所以对它们可以做什么没有设定限制。」

可以实践检验下~

➜  ~ php -r "print_r(stream_get_wrappers());"

Array

(

    [0] => https

    [1] => ftps

    [2] => compress.zlib

    [3] => compress.bzip2

    [4] => php

    [5] => file

    [6] => glob

    [7] => data

    [8] => http

    [9] => ftp

    [10] => phar

    [11] => zip

)

➜  ~

这其中,使用stream用过的有:

php,// 使用file_get_contents('php://input'); // 用来接收来自客户端的json格式请求

如:

当前端使用content-type: application/json格式

后端PHP接收请求参数的方式:

$data = json_decode(file_get_contents("php://input") ,true);

3. Stream扩展可以做些什么呢?

TBD

后续补充

...

3.问题/补充

TBD

4.参考

参见上面文档列表

后续补充

...

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值