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
<?php Swoole\Coroutine\FastCGI\Client::call(string $url, string $path, mixed $data = '', float $timeout = -1): string
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.
The actual file that will be executed on the FPM side, usually a PHP file.
Any data which you want to send, a string or array etc.
The default is to never time out (-1) but you may set a timeout in seconds. So 1.5 means 1.5 seconds.
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.
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.
<?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.