supr3m / proxy
代理库,将请求转发到指定的URL并返回响应。
v3.0.0-beta2
2015-10-30 18:45 UTC
Requires
- guzzlehttp/guzzle: ~6.0
- psr/http-message: ^1.0
- relay/relay: ^0.2.0
- zendframework/zend-diactoros: ~1.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.4
- satooshi/php-coveralls: ~0.6
This package is not auto-updated.
Last update: 2024-09-18 18:13:23 UTC
README
这是一个HTTP/HTTPS代理脚本,将请求转发到不同的服务器并返回响应。Proxy类使用PSR7请求/响应对象作为输入/输出,并使用Guzzle进行实际的HTTP请求。
安装
使用composer安装
composer require jenssegers/proxy
示例
以下示例创建了一个基于当前浏览器请求的请求对象,并将其转发到example.com
。RemoveEncodingFilter
移除了原始响应中的编码头,以便当前Web服务器可以正确设置这些头。
use Proxy\Proxy; use Proxy\Adapter\Guzzle\GuzzleAdapter; use Proxy\Filter\RemoveEncodingFilter; use Zend\Diactoros\ServerRequestFactory; // Create a PSR7 request based on the current browser request. $request = ServerRequestFactory::fromGlobals(); // Create a guzzle client $guzzle = new GuzzleHttp\Client(); // Create the proxy instance $proxy = new Proxy(new GuzzleAdapter($guzzle)); // Add a response filter that removes the encoding headers. $proxy->filter(new RemoveEncodingFilter()); // Forward the request and get the response. $response = $proxy->forward($request)->to('http://example.com'); // Output response to the browser. (new Zend\Diactoros\Response\SapiEmitter)->emit($response);
过滤器
您可以使用中间件策略对请求和响应应用过滤器
$response = $proxy ->forward($request) ->filter(function ($request, $response, $next) { // Manipulate the request object. $request = $request->withHeader('User-Agent', 'FishBot/1.0'); // Call the next item in the middleware. $response = $next($request, $response); // Manipulate the response object. $response = $response->withHeader('X-Proxy-Foo', 'Bar'); return $response; }) ->to('http://example.com');