How to Use Workerman 5.0 Async HTTP Client with Revolt Event Loop in PHP
This guide explains how to install Workerman 5.0, set up the Revolt event‑loop and the workerman/http-client component, and write PHP code that performs synchronous‑style asynchronous HTTP requests, including a concurrent benchmark example and a link to the GitHub repository.
Overview
Workerman 5.0 provides an asynchronous HTTP coroutine client for PHP. It uses PHP 8.2+ fibers to perform non‑blocking HTTP requests while allowing developers to write code in a synchronous style, improving readability and supporting high concurrency.
Key Features
Asynchronous non‑blocking requests and responses.
Built‑in connection pool for TCP reuse.
PSR‑7 compliant request and response objects.
Supports HTTP, HTTPS, WebSocket, WSS and other protocols.
Installation
Workerman 5.0 requires PHP ≥ 8.2.4. Verify your PHP version with php -v.
composer require workerman/workerman v5.0.0-beta.7Install the Revolt event‑loop package: composer require revolt/event-loop Install the HTTP client component:
composer require workerman/http-clientBasic Usage
Example http.php creates a Workerman\Worker, initializes a Workerman\Http\Client, and performs GET, POST and custom requests.
<?php
declare(strict_types=1);
use Workerman\Worker;
require_once '../vendor/autoload.php';
$worker = new Worker();
$worker->onWorkerStart = function () {
$http = new Workerman\Http\Client();
$response = $http->get('https://www.tinywan.com/');
var_dump($response->getStatusCode());
echo $response->getBody() . PHP_EOL;
$response = $http->post('https://www.tinywan.com/', ['key1'=>'value1','key2'=>'value2']);
var_dump($response->getStatusCode());
echo $response->getBody() . PHP_EOL;
$response = $http->request('https://www.tinywan.com/', [
'method' => 'GET',
'version' => '1.1',
'headers' => ['Connection' => 'keep-alive'],
'data' => ['key1'=>'value1','key2'=>'value2'],
]);
echo $response->getBody() . PHP_EOL;
};
Worker::runAll();
?>Running the Worker
/var/www/workerman5.x-demo/coroutine # php http.php start
Workerman[http.php] start in USER mode
...
Press Ctrl+C to stop. Start success.Event‑loop class used:
Workerman\Events\RevoltConcurrent Requests Example
This script creates an HTTP worker listening on port 8217, spawns 8 processes, and issues 50 parallel GET requests to an API, measuring latency for each request.
<?php
declare(strict_types=1);
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once '../vendor/autoload.php';
$httpWorker = new Worker('http://0.0.0.0:8217');
$httpWorker->count = 8;
$httpWorker->onMessage = function (TcpConnection $connection, Request $request) {
$http = new Workerman\Http\Client();
$count = 50;
$result = [];
while ($count--) {
$start = microtime(true);
$response = $http->get('https://api.tinywan.com/systems/website');
$end = microtime(true);
$result[] = sprintf('No%d | %.3f s | %d', $count, $end - $start, $response->getStatusCode());
}
$connection->send(json_encode($result));
};
Worker::runAll();
?>Sample output shows status code 200 for each request with varying response times.
Repository
Source code: https://github.com/Tinywan/workerman5.x-demo
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
