PHP code example of lisachenko / protocol-fcgi

1. Go to this page and download the library: Download lisachenko/protocol-fcgi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lisachenko / protocol-fcgi example snippets




use Lisachenko\Protocol\FCGI\FrameParser;
use Lisachenko\Protocol\FCGI\Record\BeginRequest;
use Lisachenko\Protocol\FCGI\Record\EndRequest;
use Lisachenko\Protocol\FCGI\Record\Params;
use Lisachenko\Protocol\FCGI\Record\Stdin;
use Lisachenko\Protocol\FCGI\Record\Stdout;
use Lisachenko\Protocol\FCGI\Role;

est(Role::Responder);
$packet .= new Params(['SCRIPT_FILENAME' => '/var/www/some_file.php']);
$packet .= new Params([]);
$packet .= new Stdin('');

fwrite($phpSocket, $packet);

// Read the response incrementally: the parser consumes complete frames
// from the buffer and leaves partial ones for the next read.
$buffer = '';
while ($partialData = fread($phpSocket, 4096)) {
    $buffer .= $partialData;
    while (FrameParser::hasFrame($buffer)) {
        $record = FrameParser::parseFrame($buffer);
        if ($record instanceof Stdout) {
            echo $record->getContentData();
        }
        if ($record instanceof EndRequest) {
            break 2; // response is complete
        }
    }
}

fclose($phpSocket);



use Lisachenko\Protocol\FCGI\FrameParser;

9001', $errorNumber, $errorString);

// Accept one connection and parse everything the web server sends
$socket = stream_socket_accept($server);

$buffer = '';
while ($partialData = fread($socket, 4096)) {
    $buffer .= $partialData;
    while (FrameParser::hasFrame($buffer)) {
        $record = FrameParser::parseFrame($buffer);
        var_dump($record); // BeginRequest, Params, Stdin, ...
    }
}

// Answering (Stdout + EndRequest records) is up to your application

fclose($socket);
fclose($server);