sanprojects / interceptor
拦截 PHP 函数
1.0.17
2024-07-10 20:15 UTC
Requires
- php: >=8.1
Requires (Dev)
- ext-mysqli: *
- phpunit/phpunit: >=7
- predis/predis: ^1.1
Suggests
- ext-curl: *
- ext-pdo: *
README
在 PHP 脚本中拦截外部请求,并将其记录到 STDERR
。这样您就可以更好地了解您的程序正在做什么。
安装
composer require sanprojects/interceptor
基本用法
// Intercept all kind of requests \Sanprojects\Interceptor\Interceptor::interceptAll();
现在在控制台您将看到类似以下内容
Interceptor.DEBUG: curl -vX POST 'https://www.example.com/' \
--data 'postvar1=value1&postvar2=value2&postvar3=value3'
Interceptor.DEBUG: CURL> <!doctype html> ...
Interceptor.DEBUG: fopen /Users/san/PhpstormProjects/interceptor/tests/test.txt w+b [resource(stream)]
Interceptor.DEBUG: fwrite /Users/san/PhpstormProjects/interceptor/tests/test.txt test 4
Interceptor.DEBUG: fread /Users/san/PhpstormProjects/interceptor/tests/test.txt 100 test
Interceptor.DEBUG: file_put_contents /Users/san/PhpstormProjects/interceptor/tests/test.txt test 4
Interceptor.DEBUG: Redis::__construct NULL
Interceptor.DEBUG: Redis tcp://127.0.0.1:6379 set test {"jsonKey":123} Predis\Response\Status
Interceptor.DEBUG: Redis tcp://127.0.0.1:6379 get test {"jsonKey":123}
Interceptor.DEBUG: mysqli_connect ensembldb.ensembl.org anonymous mysqli
Interceptor.DEBUG: mysqli_query ensembldb.ensembl.org SELECT 123 mysqli_result
Interceptor.DEBUG: PDO::__construct mysql:dbname=;host=ensembldb.ensembl.org anonymous NULL
Interceptor.DEBUG: PDO::query SELECT 123 Sanprojects\Interceptor\Hooks\PDOStatement
Interceptor.DEBUG: PDOStatement::execute SELECT 123; true
工作原理
它使用 stream_wrapper_register
拦截包含的 PHP 文件,并使用 stream_filter_register
来重写源代码。
支持
curl、fwrite、fread、file_get_contents、file_put_contents、mysqli、Redis、PDO、AMQP。
注意事项
- 它会关闭 opcache。
- 由于源代码注入,它可能会破坏您的应用程序。并非所有情况都已测试。
如何注入
选项 1: 运行 interceptor.phar
curl -O https://sanprojects.github.io/interceptor/interceptor.phar php -d opcache.enable=0 interceptor.phar <yourScript.php>
选项 2: 使用 auto_prepend_file
curl -O https://sanprojects.github.io/interceptor/interceptor.phar php -d opcache.enable=0 -d auto_prepend_file=interceptor.php <yourScript.php>
选项 1: 在您的脚本中包含
\Sanprojects\Interceptor\Interceptor::interceptAll();
选项 2: 在您的脚本中包含 PHP 文件
require 'vendor/bin/interceptor.php';
仅用于调试环境。