定制 khaiphan9x/fastcgi 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

khaiphan9x/fastcgi

Composer 安装命令:

composer require khaiphan9x/fastcgi

包简介

FastCGI Client implementation for PHP

README 文档

README

Enables you to request script execution from a FastCGI server. FastCGI is a protocol commonly used for web servers to request script execution from a FastCGI daemoni like php-fpm.

Use Cases

  • Executing any script that benefits from APC from the command line.
  • Running code with an independent execution context from long running daemons
  • More

Installation

$ composer require khaiphan9x/fastcgi

Usage

The client supports either tcp:

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

Or unix sockets:

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

A FastCGI request is made using an array of environment parameters and a string containing the request content. When connecting to php-fpm the $environment will be available in $_SERVER. The following environment is the minimum required by php-fpm to execute a script.

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

Once you have issued a request you must then fetch the response. The response method will block untill the request is complete.

$response = $client->response();

The response method returns an array with four keys: statusCode, headers, body, and stderr.

Currently you MUST call response before calling request again. Failure to do so will result in undefined behavior. The FastCGI protocol does support multiplexing so this may change in the future.

Exceptions

Any communication failures with the FastCGI daemon will result in an exception. The FastCGI CommunicationException extends from the SPL RuntimeException.

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

}

Common environment variables passed over FastCGI

GATEWAY_INTERFACE

variable describing the version of the fastcgi protocol to use. Currently this client supports version 1.0.

'GATEWAY_INTERFACE' => 'FastCGI/1.0'

REQUEST_METHOD

Required environment variable containing the method of the HTTP request.
Possible values include GET, HEAD, POST, PUT and DELETE.

'REQUEST_METHOD' => 'GET'

SCRIPT_FILENAME

Required environment variables containing the absolute path to the script being executed. This path must be accessible by the FastCGI server.

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

QUERY_STRING

Optionally contains a query string. You can use the http_build_query function to format an array of key/value pairs in the expected format.

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

SERVER_SOFTWARE

The software used to make the FastCGI request.

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

SERVER_NAME

The name of the server making this request

'SERVER_NAME' => php_uname('n')

CONTENT_TYPE

When sending content along with the request, this specifies the mime type of the content.

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

CONTENT_LENGTH

When sending content along with the request, this specifies the length of the content.

'CONTENT_LENGTH' => strlen($content)

Examples

Passing HTTP headers

Http headers are sent prefixed with HTTP_.

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

POST Request

$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
);

Response from php-fpm on invalid SCRIPT_FILENAME

array(4) {
  ["statusCode"]=>
  int(404)
  ["statusBody"]=>
  string(2) "OK"
  ["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
"
}

Response from php-fpm on successfull execution

array(4) {
  ["statusCode"]=>
  int(200)
  ["statusBody"]=>
  string(2) "OK"
  ["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) ""
}

khaiphan9x/fastcgi 适用场景与选型建议

khaiphan9x/fastcgi 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「fastcgi」 「php-fpm」 「fpm」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 khaiphan9x/fastcgi 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 khaiphan9x/fastcgi 我们能提供哪些服务?
定制开发 / 二次开发

基于 khaiphan9x/fastcgi 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2020-05-01