Open Swoole Coroutine FastCGI Client

Notice: ext-swoole is supported until v4.7.1, use ext-openswoole >= v4.7.1. Latest version: pecl install openswoole-4.8.1

Swoole has support for a coroutine enabled FastCGI Client that can be used to communicate with FastCGI servers like PHP-FPM, this allows you to interact with PHP-FPM servers without having to go through HTTP reverse proxies, you directly communicate through FastCGI.

You can also use the FastCGI Proxy to proxy HTTP request to your FastCGI server.

Version: Swoole: 4.5.0+


Quick Start Example

A simple FastCGI example starts with the actual PHP script, the following code example is saved within /tmp/greeter.php which is running on a PHP-FPM server.

<?php

// /tmp/greeter.php

echo 'Hello ' . ($_POST['who'] ?? 'World');

Next, on the Swoole side, we have another file saved under /tmp/swoole-fastcgi-client.php which we can execute with php /tmp/swoole-fastcgi-client.php. This calls the PHP-FPM server using the FastCGI client, no HTTP communication necessary.

<?php

// /tmp/swoole-fastcgi-client.php

Co\run(function()
{
    echo Swoole\Coroutine\FastCGI\Client::call(
        '127.0.0.1:9000',       // PHP-FPM address, can also be a socket like unix://path/to/fpm.sock
        '/tmp/greeter.php',     // The PHP script to be executed, use an absolute path
        ['who' => 'Swoole'],    // POST Request Data as an array
    );
});

When you execute php /tmp/swoole-fastcgi-client.php this will run the Swoole FastCGI client and communicate with the PHP-FPM server, run the specified PHP script, capture the result and echo it out. The call method is a quick way to execute such calls using the FastCGI communication protocol. For more flexibility and better control over the request, checkout the actual client methods and object below.

You must always execute FastCGI methods within a coroutine context, learn more about Co\run here


Other Examples


Methods

Quick Call

Main Client


Request and Response Classes

When using the Swoole FastCGI Client you will have to prepare a request using the provided objects, once you have made a successful request you will be given a response object in return. These objects allow you to easily interact with the request and response stages with the client, they have a range of functions to help setup a request and interact with a response.


Dealing with Errors

The Swoole FastCGI Client will throw an exception whenever there is a problem, you should try-catch your calls and catch Swoole\Coroutine\FastCGI\Client\Exception. You can then get the error code and message to see what went wrong or process any error conditions.

For example:

<?php

try
{
    // Your FastCGI Client Code...
}
catch(Swoole\Coroutine\FastCGI\Client\Exception $exception)
{
    echo "Error Code: {$exception->getCode()}\n";
    echo "Error Message: {$exception->getMessage()}\n";
}


Client Timeouts

All network requests (establish a connection, send data and receive data) may time out. This client has a few ways for how you can setup timeouts.

As each Swoole client is written as a built-in class using coroutines, their timeouts are set the same way, refer to the timeout guide to understand how to setup timeouts.