A PHP fast CGI client for sending requests (a)synchronously to PHP-FPM
A PHP fast CGI client for sending requests (a)synchronously to PHP-FPM
A PHP fast CGI client to send requests (a)synchronously to PHP-FPM using the FastCGI Protocol.
This library is based on the work of Pierrick Charron's PHP-FastCGI-Client and was ported and modernized to latest PHP versions, extended with some features for handling multiple requests (in loops) and unit and integration tests as well.
This is the documentation of the latest release.
Please have a look at the backwards incompatible changes (BC breaks) in the changelog.
Please see the following links for earlier releases:
Read more about the journey to and changes in v2.6.0
in this blog post.
You can find an experimental use-case in my related blog posts:
You can also find slides of my talks about this project on speakerdeck.com.
composer require hollodotme/fast-cgi-clientThis library supports two types of connecting to a FastCGI server:
'value']);
$request = new PostRequest('/path/to/target/script.php', $content);
$response = $client->sendRequest($connection, $request);
echo $response->getBody();# prints
value 'value']);
$request = new PostRequest('/path/to/target/script.php', $content);
$socketId = $client->sendAsyncRequest($connection, $request);
echo "Request sent, got ID: {$socketId}"; 'value']);
$request = new PostRequest('/path/to/target/script.php', $content);
$socketId = $client->sendAsyncRequest($connection, $request);
echo "Request sent, got ID: {$socketId}";
# Do something else here in the meanwhile
# Blocking call until response is received or read timed out
$response = $client->readResponse(
$socketId, # The socket ID
3000 # Optional timeout to wait for response,
# defaults to read/write timeout in milliseconds set in connection
);
echo $response->getBody();# prints
valueYou can register response and failure callbacks for each request. In order to notify the callbacks when a response was
received instead of returning it, you need to use the waitForResponse(int $socketId, ?int $timeoutMs = null) method.
…# prints
value…# prints
1
2
3…# prints
...............................................3
...............................................2
...............................................1…# prints
3
2
1It may be useful to see the progression of a requested script by having access to the flushed output of that script. The
php.ini default output buffering for php-fpm is 4096 bytes and is (hard-coded) disabled for CLI
mode. (See documentation)
Calling ob_implicit_flush() causes every call to echo or print to immediately be flushed.
The callee script could look like this:
addPassThroughCallbacks( $passThroughCallback );
$client->sendAsyncRequest($connection, $request);
$client->waitForResponses();# prints immediately
Buffer: Content-type: text/html; charset=UTF-8
Output: One
# sleeps 1 sec
Output: Two
# sleeps 1 sec
Output: Three
# sleeps 1 sec
Error: Oh oh!
Output: EndRequests are defined by the following interface:
[
'one',
'two' => 'value2',
'three' => [
'value3',
],
],
]
);
$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $urlEncodedContent );
$response = $client->sendRequest( $connection, $postRequest );This example produces the following $_POST array at the target script:
Array
(
[nested] => Array
(
[0] => one
[two] => value2
[three] => Array
(
[0] => value3
)
)
)Multipart form-data can be used to transfer any binary data as files to the target script just like a file upload in a browser does.
PLEASE NOTE: Multipart form-data content type works with POST requests only.
'value',
'nested[]' => 'one',
'nested[two]' => 'value2',
'nested[three][]' => 'value3',
],
# FILES
[
'file1' => __FILE__,
'files[1]' => __FILE__,
'files[three]' => __FILE__,
]
);
$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $multipartContent );
$response = $client->sendRequest( $connection, $postRequest );This example produces the following $_POST and $_FILES array at the target script:
… [
'one',
'two' => 'value2',
'three' => [
'value3',
],
],
]
);
$postRequest = PostRequest::newWithRequestContent( '/path/to/target/script.php', $jsonContent );
$response = $client->sendRequest( $connection, $postRequest );This example produces the following content for php://input at the target script:
{
"nested": {
"0": "one",
"two": "value2",
"three": [
"value3"
]
}
}Responses are defined by the following interface:
…This response is generated by php-fpm for the preceding error Primary script unknown in case the requested script does
not exists or there are path traversals in its path like /var/www/../run/script.php.
Although the given path may exist and would resolve to an absolute path in the file system, php-fpm does not do any path resolution and accepts only absolute paths to the script you want to execute.
Programatically you can handle this error like this:
if (preg_match("#^Primary script unknown\n?$#", $response->getError()))
{
throw new Exception('Could not find or resolve path to script for execution.');
}
# OR
if ('404 Not Found' === $response->getHeaderLine('Status'))
{
throw new Exception('Could not find or resolve path to script for execution.');
}
# OR
if ('File not found.' === trim($response->getBody()))
{
throw new Exception('Could not find or resolve path to script for execution.');
}This requires docker and docker-compose installed on your machine.
make updatemake examplesmake testsPlease note: bin/fcgiget is not included and linked to vendor/bin via composer anymore since version v3.1.2for
security reasons. Read more.
Run a call through a network socket:
docker-compose exec php74 php bin/fcgiget localhost:9001/statusRun a call through a Unix Domain Socket
docker-compose exec php74 php bin/fcgiget unix:///var/run/php-uds.sock/statusThis shows the response of the php-fpm status page.
No open issues yet, or sync has not completed.