如何在Node JSsubprocess中设置$ _POST值

我在AWS Lambda上托pipe了我的Slim应用程序。 对于我的PHP应用程序来说,我遵循这个教程

我的应用程序运行正常,直到我尝试使用POST方法提交表单。 我的PHP无法从表单中获取值。 当我抛弃$ _POSTfile_get_contents('php:// input')时 ,两者都返回null

在本教程中,Chris(作者)指出,这段代码产生了subprocess,并设置了PHP CGI填充到$ _SERVER超级全局variables中的一堆环境variables。

var php = spawn('./php-cgi', ['function.php'], { env: Object.assign({ REDIRECT_STATUS: 200, REQUEST_METHOD: requestMethod, SCRIPT_FILENAME: 'function.php', SCRIPT_NAME: '/function.php', PATH_INFO: '/', SERVER_NAME: serverName, SERVER_PROTOCOL: 'HTTP/1.1', REQUEST_URI: requestUri }, headers) }); 

我不熟悉subprocess,所以我想问一下,是否还有一种方法可以填充$ _POST超全局? 因为我认为POST数据存在于我的处理程序函数中的事件对象/variables中,意思是(我想)我的NodeJS包装器可以访问POST数据,但是它没有传递给PHP CGI?

 exports.handler = function(event, context) 

这里是我的NodeJS包装的完整代码:

 var spawn = require('child_process').spawn; var parseHeaders, parseResponse, parseStatusLine; parseResponse = function(responseString) { var headerLines, line, lines, parsedStatusLine, response; response = {}; lines = responseString.split('\r\n'); parsedStatusLine = parseStatusLine(lines.shift()); response['protocolVersion'] = parsedStatusLine['protocol']; response['statusCode'] = parsedStatusLine['statusCode']; response['statusMessage'] = parsedStatusLine['statusMessage']; headerLines = []; while (lines.length > 0) { line = lines.shift(); if (line === "") { break; } headerLines.push(line); } response['headers'] = parseHeaders(headerLines); response['body'] = lines.join('\r\n'); return response; }; parseHeaders = function(headerLines) { var headers, key, line, parts, _i, _len; headers = {}; for (_i = 0, _len = headerLines.length; _i < _len; _i++) { line = headerLines[_i]; parts = line.split(":"); key = parts.shift(); headers[key] = parts.join(":").trim(); } return headers; }; parseStatusLine = function(statusLine) { var parsed, parts; parts = statusLine.match(/^(.+) ([0-9]{3}) (.*)$/); parsed = {}; if (parts !== null) { parsed['protocol'] = parts[1]; parsed['statusCode'] = parts[2]; parsed['statusMessage'] = parts[3]; } return parsed; }; exports.index = function(event, context) { // Sets some sane defaults here so that this function doesn't fail when it's not handling a HTTP request from // API Gateway. var requestMethod = event.httpMethod || 'GET'; var serverName = event.headers ? event.headers.Host : ''; var requestUri = event.path || ''; var headers = {}; // Convert all headers passed by API Gateway into the correct format for PHP CGI. This means converting a header // such as "X-Test" into "HTTP_X-TEST". if (event.headers) { Object.keys(event.headers).map(function (key) { headers['HTTP_' + key.toUpperCase()] = event.headers[key]; }); } // Spawn the PHP CGI process with a bunch of environment variables that describe the request. var php = spawn('./php-cgi', ['slim/public/index.php'], { env: Object.assign({ REDIRECT_STATUS: 200, REQUEST_METHOD: requestMethod, SCRIPT_FILENAME: 'slim/public/index.php', SCRIPT_NAME: '/index.php', PATH_INFO: '/', SERVER_NAME: serverName, SERVER_PROTOCOL: 'HTTP/1.1', REQUEST_URI: requestUri }, headers) }); // Listen for output on stdout, this is the HTTP response. var response = ''; php.stdout.on('data', function(data) { response += data.toString('utf-8'); }); // When the process exists, we should have a complete HTTP response to send back to API Gateway. php.on('close', function(code) { // Parses a raw HTTP response into an object that we can manipulate into the required format. var parsedResponse = parseResponse(response); // Signals the end of the Lambda function, and passes the provided object back to API Gateway. context.succeed({ statusCode: parsedResponse.statusCode || 200, headers: parsedResponse.headers, body: parsedResponse.body }); }); };