如果您只需要支持流包装器(即不使用套接字函数),则应该可以取消注册http
流包装器并添加您自己的拦截调用然后转发它们。这是一个例子:
<?php
class HttpCaptureStream
{
private $fd;
public static $captured;
public static function reset()
{
static::$captured = array();
}
public function stream_open($path, $mode, $options, &$opened_path)
{
static::$captured[] = $path;
$this->fd = $this->restored(function () use ($path, $mode) {
return fopen($path, $mode);
});
return (bool) $this->fd;
}
public function stream_read($count)
{
return fread($this->fd, $count);
}
public function stream_write($data)
{
return fwrite($this->fd, $data);
}
public function stream_tell()
{
return ftell($this->fd);
}
public function stream_eof()
{
return feof($this->fd);
}
public function stream_seek($offset, $whence)
{
return fseek($this->fd, $offset, $whence);
}
public function stream_stat()
{
return fstat($this->fd);
}
protected function restored($f)
{
stream_wrapper_restore('http');
$result = $f();
stream_wrapper_unregister('http');
stream_wrapper_register('http', __CLASS__);
return $result;
}
}
function capture($f) {
stream_wrapper_unregister('http');
stream_wrapper_register('http', 'HttpCaptureStream');
HttpCaptureStream::reset();
$f();
stream_wrapper_restore('http');
}
capture(function () {
file_get_contents('http://google.com');
file_get_contents('http://stackoverflow.com');
var_dump(HttpCaptureStream::$captured);
});