Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST to PHP from Node.js

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.

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.

[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. 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'); does work to get the data on the PHP side but I would still prefer to be able to access the $_POST array directly.

like image 425
niczak Avatar asked Jan 29 '14 20:01

niczak


1 Answers

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.

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();
like image 82
Patrick Evans Avatar answered Nov 17 '22 19:11

Patrick Evans