stream_context_create:
使用这个方法,就可以更容易的模拟GET、POST请求了,当然,他们作用不只局限于这些,演示代码:
'method'=>"GET",
'header'=>"Accept-language: en\r\n" .
"Cookie: foo=bar\r\n"
)
);
/* Sends an http request to www.example.com
with additional headers shown above */
$fp =
fopen('http://www.example.com',
'r',
false,
$context);
?>
stream_wrapper_register:
简单的说,就是实现了一个钩子,在使用一些流相关的函数的时候,这些预定义的钩子会被自动执行。
闲言碎语不多讲,看代码:
class Foobar {
function stream_open($path) {
echo 'The path is ' .
$path;
return true;
}
function stream_read() {
// ...
}
function stream_eof() {
// ...
}
// ...
}
include('demo://abc.php');
当你执行上面代码的时候,会发现include操作触发了stream_open,打印出“The path is demo://abc.php“,当然,这样的代码没有实际的应用价值,但是改造一下,我们就能用stream_wrapper_register写出很 炫的东西来,还拿include操作来说,我们还可以实现类似下面的操作:
include('xml://some_xml_file.php');
include('yml://some_yml_file.php');
在include的时候,按照我们定义的协议(xml/yml)去执行预先定义的钩子动作,比如说按照特定的格式解析文件并返回PHP能识别的变量等等。
我们还可以玩得更出格些,比如:
include('aop://class.php');
然后,可以按照我们预先设置好的配置文件完成对class.php的预编译以实现AOP的效果。
反正,没有做不到,只有想不到。
当然,除了include,其他如fopen, fread等流相关函数都可以,大家发挥自己的想象吧。