Swoole FastCGI Client PSR Style

4.x is outdated, please check the latest version 22.x

Latest version: pecl install openswoole-22.1.2

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.



use Swoole\FastCGI\HttpRequest;
use Swoole\FastCGI\HttpResponse;
use Swoole\Coroutine\FastCGI\Client;

// The FastCGI client must run within a coroutine context
    // We use a try-catch to process any errors if something goes wrong
        // Create a new client based on host and port, can also use a UnixSocket
        $client = new Swoole\Coroutine\FastCGI\Client('', 9000);

        // Builds up the request we want to send, with many options...
        $request = (new Swoole\FastCGI\HttpRequest())
            ->withScriptFilename(__DIR__ . '/greeter.php')
            ->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";
Last updated on August 31, 2022