owner888 / php-http
Pure-PHP HTTP/1.1 and HTTP/2 client (ALPN, HPACK, pools, gzip, proxies) and WebSocket client, powered by owner888/php-async.
Requires
- php: >=8.3
- owner888/php-async: ^1.0
Requires (Dev)
- phpunit/phpunit: ^11.0 || ^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Pure-PHP 8.3+ HTTP/1.1 & HTTP/2 client (ALPN negotiation, HPACK header compression, multiplexed streams, connection pools, gzip decompression, HTTP CONNECT & SOCKS5 proxies, chunked upload progress), and WebSocket client. Socket I/O, DNS, TLS, Fibers and scheduling are provided by the standalone owner888/php-async dependency, with no HTTP-owned event loop.
Shared by MyPHP and pig. No ext-curl or C extension required beyond PHP's standard ext-openssl.
Installation
composer require owner888/php-http
Quick Start
Concurrent HTTP/2 Requests with Fibers
<?php use Owner888\Async\Async; use Owner888\Http\{HttpClient, Request}; Async::run(function (): void { $client = new HttpClient(timeout: 15.0); // 8 concurrent requests multiplexed over a single HTTP/2 connection: $futures = []; for ($i = 0; $i < 8; $i++) { $futures[] = Async::spawn(function () use ($client, $i): string { $res = $client->follow(new Request( 'GET', "https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=5m&limit=1000", ['Accept-Encoding' => 'gzip'], )); return $res->body->all(); }); } foreach ($futures as $f) { $body = $f->await(); echo strlen($body), " bytes\n"; } });
Embedding in an existing event loop
The owner888/php-async dependency owns all readiness watchers, timers, and deferred callbacks. Standalone clients use its native loop; hosts such as Workerman can install their own Scheduler once and call HttpClient::send() or follow() inside the host Fiber. No secondary loop is created. See php-async's embedding guide for the Revolt adapter and GUI integration.
Proxies & Custom CA Certificates
use Owner888\Async\Socket; use Owner888\Http\{HttpClient, Proxy}; // Honor HTTP_PROXY / HTTPS_PROXY / ALL_PROXY / NO_PROXY: $client = new HttpClient(proxy: Proxy::fromEnvironment(getenv())); // Or explicit HTTP / SOCKS5 proxy: $client = new HttpClient(proxy: Proxy::parse('socks5://127.0.0.1:1080')); // Trust an additional CA certificate (or set SSL_CERT_FILE in the environment): Socket::useExtraCaFile('/path/to/custom-ca.pem');
WebSocket Client
use Owner888\Async\Async; use Owner888\Http\WebSocket; Async::run(function (): void { $ws = WebSocket::connect('wss://echo.websocket.events', []); $ws->send('hello'); echo $ws->receive(), "\n"; $ws->close(); });
License
MIT