问题描述
我正在尝试从Node.js应用程序将一些数据POST到PHP脚本。暂时我只是构建了一个概念证明,但是我无法将实际的数据转移到PHP端。请求通过,我得到200回,但PHP认为$ _POST数组是空的。
I am trying to POST some data from a Node.js application to a PHP script. For the time being I am just building a proof of concept but I am unable to get the actual data over to the PHP side. The request goes through and I get 200 back but PHP thinks the $_POST array is empty.
这是我的节点代码:
// simple end point just for testing
exports.testPost = function(request, response) {
data = request.body.data;
postToPHP(data);
response.end(data);
}
function postToPHP (data) {
var http = require('http');
var options = {
host : 'localhost',
port : 8050,
path : '/machines/test/index.php',
method : 'POST',
headers : {
'Content-Type' : 'application/json',
'Content-Length' : Buffer.byteLength(data)
}
};
var buffer = "";
var reqPost = http.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
res.on('data', function(d) {
console.info('POST Result:\n');
//process.stdout.write(d);
buffer = buffer+data;
console.info('\n\nPOST completed');
});
res.on('end', function() {
console.log(buffer);
});
});
console.log("before write: "+data);
reqPost.write(data);
reqPost.end();
}
再次,请求使其到localhost:8050 /机器/test/index.php,但是当我做一个$ _POST的var_dump它是一个空数组。
Again, the request makes it to localhost:8050/machines/test/index.php but when I do a var_dump of $_POST it is an empty array.
[29-Jan-2014 21:12:44] array(0) {
}
我怀疑我做错了.write()方法,但我不能弄清楚什么。任何关于我失踪或做错误的输入将不胜感激。
I suspect I am doing something wrong with the .write() method but I can't quite figure out what. Any input on what I am missing or doing incorrectly would be greatly appreciated.
* 更新:
由于某些注释表示使用file_get_contents(' PHP://输入');确实可以在PHP方面获取数据,但我仍然希望直接访问$ _POST数组。
As some of the comments indicate using file_get_contents('php://input'); does work to get the data on the PHP side but I would still prefer to be able to access the $_POST array directly.
推荐答案
由于您以内容类型发送数据:application / json
你需要读取原始输入,因为php不知道如何将json读入全局变量,如_GET和_POST,除非你有一些php扩展名。
Since you are sending the data with Content-Type: application/json
you would need to read the raw input as php does not know how to read json into their globals like _GET and _POST unless you have some php extension that does it.
您可以使用库将对象解析为名称 - 值对查询字符串,您可以使用内容类型:application / x-www-form-urlencoded
,以便将数据解析为全局变量
You can use the querystring library to parse a object into a name-value pair query string that you could than transmit with Content-Type:application/x-www-form-urlencoded
so that the data will be parsed into the globals
var data = {
var1:"something",
var2:"something else"
};
var querystring = require("querystring");
var qs = querystring.stringify(data);
var qslength = qs.length;
var options = {
hostname: "example.com",
port: 80,
path: "some.php",
method: 'POST',
headers:{
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': qslength
}
};
var buffer = "";
var req = http.request(options, function(res) {
res.on('data', function (chunk) {
buffer+=chunk;
});
res.on('end', function() {
console.log(buffer);
});
});
req.write(qs);
req.end();
这篇关于POST到PHP从Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!