Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 25.x
Latest version:
pecl install openswoole-25.2.0
The Swoole FastCGI Client supports coroutines when executed inside a coroutine context like Co\run
or within a go()
call. The client also has support for PSR Style.
<?php
use Swoole\FastCGI\HttpRequest;
use Swoole\FastCGI\HttpResponse;
use Swoole\Coroutine\FastCGI\Client;
// The FastCGI client must run within a coroutine context
Co\run(function()
{
// We use a try-catch to process any errors if something goes wrong
try
{
// Create a new client based on host and port, can also use a UnixSocket
$client = new Swoole\Coroutine\FastCGI\Client('127.0.0.1', 9000);
// Builds up the request we want to send, with many options...
$request = (new Swoole\FastCGI\HttpRequest())
->withScriptFilename(__DIR__ . '/greeter.php')
->withMethod('POST')
->withBody(['who' => 'Swoole']);
// Returns a Swoole\FastCGI\HttpResponse
$response = $client->execute($request);
echo "Result: {$response->getBody()}\n";
}
catch(Swoole\Coroutine\FastCGI\Client\Exception $exception)
{
echo "Error Code: {$exception->getCode()}\n";
echo "Error Message: {$exception->getMessage()}\n";
}
});