如何在Node JS子进程中设置$ _POST值

时间:2017-08-31 03:25:13

标签: php node.js amazon-web-services http-post child-process

我在AWS Lambda上托管了我的Slim应用程序。为了使我的PHP应用程序正常工作,我遵循了this tutorial

我的应用运行正常,直到我尝试使用POST方法提交表单。我的PHP无法从表单中获取值。当我转储 $ _ POST file_get_contents(' php://输入')时,两者都返回了 null

在本教程中,Chris(作者)声明此代码生成子进程并设置一组环境变量,PHP CGI将这些变量填充到$ _SERVER超级全局中。

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

我不熟悉子进程,所以我想问一下我是否还可以填充 $ _ POST 超全局?因为我认为POST数据存在于我的处理函数中的事件对象/变量中,这意味着(我认为)我的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
        });
    });
};

1 个答案:

答案 0 :(得分:1)

在某些情况下,有必要在环境中设置CONTENT_LENGTH和/或CONTENT_TYPE,以便php-cgi能够正确处理$ _POST。
例如(其中postBody是类似“ field1 = value1&field2 = value2”的字符串):

var env = {
    'SCRIPT_FILENAME': script_path, 
    'REQUEST_METHOD': 'POST',
    'REDIRECT_STATUS': 1,  
    'CONTENT_TYPE': 'application/x-www-form-urlencoded', 
    'CONTENT_LENGTH': postBody.length  
}

//if the URL has anything after "?", it should appear in $_GET even when the method is "POST"
if(queryString) env['QUERY_STRING'] = queryString; 

帖子正文需要输入到孩子的stdin中。
这是一个异步示例:

var spawn = require('child_process').spawn; 
var phpProcess = spawn (php_cgi_path, [script_path], {'env': env})
phpProcess.stdin.write( postBody );

var outputBuffer = []; 
phpProcess.stdout.on('data', function(data) { 
    outputBuffer.push (data.toString()); 
}) 
phpProcess.stdout.on('end', function( ) { 
    var phpOutput = outputBuffer.join('') ; 
    // process php output
});

也可以以同步方式提供输入数据,例如:

var spawnSync = require('child_process').spawnSync; 
var phpProcessSync = spawnSync (php_cgi_path, [script_path], {'env': env, 'input': postBody})
var phpOutput = phpProcessSync.stdout.toString()
// process php output

同样,帖子数据与“ env”分开输入。

还可以修改脚本,使其也填充$ _FILES。
例如,可以使用Uint8Array(而不是字符串)来存储帖子正文,然后将'CONTENT_TYPE'设置为request.headers['content-type']
(然后我们会有"Content-Type:multipart/form-data; boundary=----WebKitFormBoundarynGa8p8HMIQ8kWQLA"
然后使用phpProcess.stdin.write( Buffer.from(postBody) ); $ _FILES变量中将包含“ tmp_name”等。