clue/http-proxy-react
Async HTTP proxy with support for forward and reverse proxying (including HTTPS via CONNECT), built on ReactPHP. Useful for tunneling, debugging and routing HTTP(S) traffic in event-driven PHP apps, with streaming I/O and low overhead.
To integrate clue/http-proxy-react in a Laravel application, start by installing the package via Composer:
composer require clue/http-proxy-react react/socket react/http
Leverage the package to route HTTPS traffic through a proxy (e.g., for scraping or bypassing geo-restrictions). Example:
use Clue\React\HttpProxy\ProxyConnector;
use React\Socket\Connector;
use React\Http\Browser;
public function proxyRequest()
{
$loop = React\EventLoop\Factory::create();
$proxy = new ProxyConnector('proxy.example.com:8080');
$connector = new Connector(['tcp' => $proxy, 'dns' => false], $loop);
$browser = new Browser($connector);
$browser->get('https://example.com')->then(
fn($response) => dd($response->getBody()->getContents()),
fn($e) => dd($e->getMessage())
);
$loop->run();
}
Key Steps:
ProxyConnector with your proxy server URL.Connector to integrate with ReactPHP's ecosystem.React\Http\Browser to send requests through the proxy.Use the ProxyConnector to tunnel non-HTTP protocols (e.g., SMTP, IMAP) through an HTTP proxy. Example:
$proxy = new ProxyConnector('proxy.example.com:8080');
$connector = new Connector(['tcp' => $proxy], $loop);
$connector->connect('tcp://smtp.example.com:25')->then(
fn($connection) => $connection->write("EHLO example.com\r\n"),
fn($e) => dd($e->getMessage())
);
Pass credentials directly in the proxy URL:
$proxy = new ProxyConnector('user:pass@proxy.example.com:8080');
Wrap the ProxyConnector in Connector to enforce timeouts:
$connector = new Connector(['tcp' => $proxy, 'timeout' => 5.0], $loop);
$connector = new Connector(['tcp' => $proxy, 'dns' => false], $loop);
$connector = new Connector(['tcp' => $proxy, 'dns' => '8.8.8.8'], $loop);
Use the package with Laravel's HTTP client by replacing the underlying connector:
use Illuminate\Support\Facades\Http;
$loop = React\EventLoop\Factory::create();
$proxy = new ProxyConnector('proxy.example.com:8080');
$connector = new Connector(['tcp' => $proxy], $loop);
Http::macro('throughProxy', function ($url) use ($connector) {
return Http::withOptions([
'connector' => $connector,
])->get($url);
});
// Usage
Http::throughProxy('https://example.com')->then(...);
gethostbyname()) before proxying.@, :) are URL-encoded:
$url = rawurlencode('user@domain') . ':' . rawurlencode('pass!') . '@proxy.example.com';
407 error. Handle this gracefully:
$proxy->connect('https://example.com')->otherwise(
fn($e) => $e->getCode() === 407 ? dd('Proxy auth failed') : dd($e)
);
React\Promise\Timer\TimeoutException to enforce timeouts:
$promise->timeout(3.0)->then(...)->otherwise(fn($e) => dd('Timeout'));
React\Promise\resolve() to bridge:
$loop->futureTick(fn() => $syncFunction());
React\EventLoop\Factory::create(['debug' => true]);
Via, X-Forwarded-For) to debug proxy hops:
$proxy = new ProxyConnector('proxy.example.com:8080', null, [
'headers' => ['Via' => 'Laravel/Proxy']
]);
$proxy = new ProxyConnector('proxy.example.com:8080', null, [
'headers' => ['User-Agent' => 'Laravel/Proxy']
]);
$proxy = new ProxyConnector('unix:///var/run/proxy.sock');
Register the proxy connector as a singleton in AppServiceProvider:
public function register()
{
$this->app->singleton('proxy.connector', fn() => new ProxyConnector(config('proxy.url')));
}
Then inject it into services:
public function __construct(private ProxyConnector $proxy) {}
How can I help you explore Laravel packages today?