ebernhardson/fastcgi

该包的最新版本(v0.2.0)没有提供许可证信息。

PHP的FastCGI客户端实现

v0.2.0 2015-09-09 03:14 UTC

This package is not auto-updated.

Last update: 2024-09-22 04:15:15 UTC


README

使您能够从FastCGI服务器请求脚本执行。FastCGI是一种被Web服务器广泛使用的协议,用于从FastCGI守护进程(如php-fpm)请求脚本执行。

用例

  • 从命令行执行任何受益于APC的脚本。
  • 从具有独立执行上下文的长时间运行守护进程运行代码
  • 更多

安装

$ composer require ebernhardson/fastcgi

用法

客户端支持tcp

$client = new \EBernhardson\FastCGI\Client('localhost', '8989');

或Unix套接字

$client = new \EBernhardson\FastCGI\Client('/var/run/php5-fpm.sock');

使用包含环境参数数组和包含请求内容的字符串发起FastCGI请求。连接到php-fpm时,$environment将在$_SERVER中可用。以下环境是php-fpm执行脚本所需的最小环境。

$environment = [
    'REQUEST_METHOD'  => 'GET',
    'SCRIPT_FILENAME' => '/full/path/to/script.php',
];
$client->request($environment, '');

一旦发出请求,就必须获取响应。response方法将在请求完成之前阻塞。

$response = $client->response();

response方法返回一个包含四个键的数组:statusCodeheadersbodystderr

目前您必须先调用response,然后再调用request。不这样做将导致未定义的行为。FastCGI协议支持多路复用,所以这可能会在未来发生变化。

异常

与FastCGI守护进程的任何通信故障都会导致异常。FastCGI的CommunicationException扩展自SPL的RuntimeException

try {
    $client->request($environment, $stdin);
    $response = $client->response();
} catch (\EBernhardson\FastCGI\CommunicationException $failure) {
    // handle failure

}

通过FastCGI传递的常见环境变量

GATEWAY_INTERFACE

描述要使用的fastcgi协议版本的变量。目前此客户端支持版本1.0。

'GATEWAY_INTERFACE' => 'FastCGI/1.0'

REQUEST_METHOD

包含HTTP请求方法的必需环境变量。
可能的值包括GETHEADPOSTPUTDELETE

'REQUEST_METHOD' => 'GET'

SCRIPT_FILENAME

包含正在执行的脚本的绝对路径的必需环境变量。此路径必须可通过FastCGI服务器访问。

'SCRIPT_FILENAME' => '/home/zomg/deploy/current/web/app.php'

QUERY_STRING

可选地包含查询字符串。您可以使用http_build_query函数以预期格式格式化键/值对数组。

'QUERY_STRING' => http_build_query(['key' => 'value'])

SERVER_SOFTWARE

用于发起FastCGI请求的软件。

'SERVER_SOFTWARE' => 'awesome-application/1.0'

SERVER_NAME

发起此请求的服务器名称

'SERVER_NAME' => php_uname('n')

CONTENT_TYPE

当与请求一起发送内容时,指定内容的MIME类型。

'CONTENT_TYPE' => 'application/x-www-form-urlencoded'

CONTENT_LENGTH

当与请求一起发送内容时,指定内容的长度。

'CONTENT_LENGTH' => strlen($content)

示例

传递HTTP头

HTTP头以HTTP_为前缀发送。

$client->request(
    [
        'REQUEST_METHOD'  => 'GET',
        'SCRIPT_FILENAME' => '/full/path/to/test.php',
        'HTTP_ACCEPT'     => 'application/json',
    ],
    ''
);

POST请求

$content = 'key=value';
$client->request(
    [
        'REQUEST_METHOD'  => 'POST',
        'SCRIPT_FILENAME' => '/full/path/to/test.php',
        'CONTENT_TYPE'    => 'application/x-www-form-urlencoded',
        'CONTENT_LENGTH'  => strlen($content)
    ],
    $content
);

php-fpm对无效的SCRIPT_FILENAME的响应

array(4) {
  ["statusCode"]=>
  int(404)
  ["headers"]=>
  array(3) {
    ["status"]=>
    string(14) "404 Not Found"
    ["x-powered-by"]=>
    string(22) "PHP/5.4.9-4~precise+1"
    ["content-type"]=>
    string(9) "text/html"
  }
  ["body"]=>
  string(15) "File not found."
  ["stderr"]=>
  string(23) "Primary script unknown
"
}

php-fpm对成功执行的响应

array(4) {
  ["statusCode"]=>
  int(200)
  ["headers"]=>
  array(3) {
    ["x-powered-by"]=>
    string(22) "PHP/5.4.9-4~precise+1"
    ["content-type"]=>
    string(9) "text/html"
    ["status"]=>
    string(6) "200 OK"
  }
  ["body"]=>
  string(11) "Hello World"
  ["stderr"]=>
  string(0) ""
}