madewithlove / htaccess-api-client
全球最佳htaccess测试器的API客户端。
v2.3.0
2021-11-26 12:52 UTC
Requires
- php: ^8.0
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
Requires (Dev)
- http-interop/http-factory-guzzle: ^1.0
- infection/infection: ^0.25
- php-http/guzzle7-adapter: ^1.0
- phpstan/phpstan: ^1.0.0
- phpunit/phpunit: ^9.1
README
这是一个用于与Htaccess测试器交互的API客户端。
安装
composer require madewithlove/htaccess-api-client
使用方法
该包可以与任何PSR兼容的HTTP客户端一起使用。在这个例子中,我们将使用guzzle的PSR适配器。
use Http\Factory\Guzzle\ServerRequestFactory; use Http\Adapter\Guzzle6\Client; use Madewithlove\HtaccessClient $client = new HtaccessClient( new Client(), new ServerRequestFactory() ); $response = $client->test( 'http://localhost', 'RewriteRule .* /foo [R]' ); $response->getOutputUrl(); // "http://localhost/foo" $response->getLines(); /* array(1) { [0]=> object(Madewithlove\ResultLine)#30 (5) { ["line":"Madewithlove\ResultLine":private]=> string(23) "RewriteRule .* /foo [R]" ["message":"Madewithlove\ResultLine":private]=> string(98) "The new url is http://localhost/foo Test are stopped, a redirect will be made with status code 302" ["isMet":"Madewithlove\ResultLine":private]=> bool(true) ["isValid":"Madewithlove\ResultLine":private]=> bool(true) ["wasReached":"Madewithlove\ResultLine":private]=> bool(true) } } */
服务器变量
Htaccess测试器支持传递服务器变量以供重写规则评估。我们目前支持以下变量。
服务器变量可以通过test()
和share()
方法传递。
$serverVariables = ServerVariables::default()->with( 'SERVER_NAME', 'example.com' ); $response = $client->test( 'http://localhost', 'RewriteCond %{SERVER_NAME} example.com RewriteRule .* /foo [R]', $serverVariables ); $response = $client->share( 'http://localhost', 'RewriteCond %{SERVER_NAME} example.com RewriteRule .* /foo [R]', $serverVariables );