Swoole\Coroutine\FastCGI\Client->call(...)

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


Latest version: pecl install openswoole-22.1.2

Declaration

<?php Swoole\Coroutine\FastCGI\Client::call(string $url, string $path, mixed $data = '', float $timeout = -1): string

Parameters

url

The FastCGI address, where the FPM process is monitoring, could be 127.0.0.1:9000 or a UnixSocket like unix:/tmp/php-cgi.sock.

path

The actual file that will be executed on the FPM side, usually a PHP file.

data

Any data which you want to send, a string or array etc.

timeout

The default is to never time out (-1) but you may set a timeout in seconds. So 1.5 means 1.5 seconds.

Return

Returns the response from the FPM request, returns the actual response body. You can catch any errors by using a try-catch with this client.


Description

The FastCGI call() method is a static helper used to make it easy to execute quick requests via FastCGI and PHP-FPM, internally it handles setting up the client, attaching data and sorting out request and response objects to use. Basically, a wrapper to the main FastCGI client object.

Either this method will return the body of the server response or an error will be thrown under the exception Swoole\Coroutine\FastCGI\Client\Exception, you can use a try-catch to process errors.


Example

<?php

// /tmp/greeter.php

echo 'Hello ' . ($_POST['who'] ?? 'World');
<?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
    );
});

Checkout the main FastCGI Client page for more details and examples.

Last updated on August 31, 2022