Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lisachenko/protocol-fcgi

FastCGI (FCGI) Protocol implementation for PHP
https://github.com/lisachenko/protocol-fcgi

fastcgi fcgi nginx php protocol protocol-fcgi

Last synced: 4 days ago
JSON representation

FastCGI (FCGI) Protocol implementation for PHP

Awesome Lists containing this project

README

        

Object-oriented implementation of FCGI Protocol for PHP
---------------------------

FastCGI is an open extension to CGI that provides high performance for all Internet applications without the penalties
of Web server APIs.

Many modern web-servers such as nginx, apache, lighthttpd, etc are communicating with PHP via FCGI. So, this protocol
is well known and used in many applications. More detailed information about the protocol is available here here:
http://www.fastcgi.com/devkit/doc/fcgi-spec.html

[![Build Status](https://travis-ci.org/lisachenko/protocol-fcgi.svg)](https://travis-ci.org/lisachenko/protocol-fcgi)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/lisachenko/protocol-fcgi/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/lisachenko/protocol-fcgi/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/lisachenko/protocol-fcgi/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/lisachenko/protocol-fcgi/?branch=master)
[![Packagist](https://img.shields.io/packagist/v/lisachenko/protocol-fcgi.svg)](https://github.com/lisachenko/protocol-fcgi)
[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%207.4-8892BF.svg)](https://php.net/)
[![License](https://img.shields.io/packagist/l/lisachenko/protocol-fcgi.svg)](https://packagist.org/packages/lisachenko/protocol-fcgi)

Usage
------------
This library can be used for implementing both client and server side of FCGI application. For example, nginx can
connect to the PHP FCGI daemon, or some library code can connect to the FPM as a FCGI client.

To install this library, just write

``` bash
$ composer require lisachenko/protocol-fcgi
```

After that you can use an API to parse/create FCGI requests and responses.

Simple FCGI-client:
```php
'/var/www/some_file.php']);
$packet .= new Params();
$packet .= new Stdin();

fwrite($phpSocket, $packet);

$response = '';
while ($partialData = fread($phpSocket, 4096)) {
$response .= $partialData;
while (FrameParser::hasFrame($response)) {
$record = FrameParser::parseFrame($response);
var_dump($record);
};
};

fclose($phpSocket);
```

To implement FCGI server, just create a socket and make request-response loop

```php

use Lisachenko\Protocol\FCGI;
use Lisachenko\Protocol\FCGI\FrameParser;
use Lisachenko\Protocol\FCGI\Record;
use Lisachenko\Protocol\FCGI\Record\BeginRequest;
use Lisachenko\Protocol\FCGI\Record\Params;
use Lisachenko\Protocol\FCGI\Record\Stdin;

include "vendor/autoload.php";

$server = stream_socket_server("tcp://127.0.0.1:9001" , $errorNumber, $errorString);

// Just take the first one request and process it
$phpSocket = stream_socket_accept($server);

$response = '';
while ($partialData = fread($phpSocket, 4096)) {
$response .= $partialData;
while (FrameParser::hasFrame($response)) {
$record = FrameParser::parseFrame($response);
var_dump($record);
};
};

// We don't respond correctly here, it's a task for your application

fclose($phpSocket);
fclose($server);
```