[英]POST to PHP from Node.js
I am trying to POST some data from a Node.js application to a PHP script. 我试图将Node.js应用程序中的一些数据发布到PHP脚本。 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.
暂时我只是构建一个概念证明,但我无法将实际数据传递给PHP端。 The request goes through and I get 200 back but PHP thinks the $_POST array is empty.
请求通过,我得到200回来,但PHP认为$ _POST数组是空的。
Here is my node code: 这是我的节点代码:
// 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();
}
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. 同样,请求进入localhost:8050 / machines / test / index.php,但当我执行$ _POST的var_dump时,它是一个空数组。
[29-Jan-2014 21:12:44] array(0) {
}
I suspect I am doing something wrong with the .write() method but I can't quite figure out what. 我怀疑我在使用.write()方法做错了但是我无法弄清楚是什么。 Any input on what I am missing or doing incorrectly would be greatly appreciated.
任何关于我遗失或做错的输入都将非常感激。
* Update: *更新:
As some of the comments indicate using file_get_contents('php://input'); 有些评论表明使用了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.
确实可以在PHP端获取数据,但我仍然希望能够直接访问$ _POST数组。
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. 由于你使用
Content-Type: application/json
发送数据,你需要读取原始输入,因为php不知道如何将json读入他们的全局变量,如_GET和_POST,除非你有一些php扩展来完成它。
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 您可以使用查询字符串库将对象解析为名称 - 值对查询字符串,您可以使用
Content-Type:application/x-www-form-urlencoded
进行传输,以便将数据解析为全局变量
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.