i am new in nodejs
i want create app use nodejs and php
the scheme like this..
nodejs-->id-->php(insert data to mysql)
nodejs send value id to php page (php receive data from nodejs).in php i want to insert data to mysql
my question is how i send value to php from nodejs so php can receive the data?
how i achieve this?any tutorial explain about this?
thanks
So I guess you essentially are wanting to move data from a node.js server on one running PHP? In that case you will need to use a web request from within node.js to the URL that your PHP instance is running on. I would recommend Mikeal's excellent request package.
You can use it like so:
var request = require('request');
//so we assume this function is called with some
//string value for id, and some string representing
//the URL for your PHP endpoint, e.g.:
//>sendIdToURL('foo', 'http://mydomain.com/myendpoint.php')
function sendIdToURL(id, url) {
request(url, {
method: 'POST',
body: id
}, function(err, response) {
if(err) throw err;
else {
//do something with the response if you want
}
});
}
Then on the PHP end your $HTTP_RAW_POST_DATA, or file_get_contents("php://input") will have the value 'foo'.
Does that answer the question? It's an interesting use case that you have to use php for the mysql connection...
Node side make a post to your php file. Like:
var postRequest = {
host: "mydomain.com",
path: "/myfile.php?do=insert&field1=somedata",
port: 80,
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
};
and php side recive these stuff like:
$_POST['...']
Then do db actions.
Heres more info: node.js POST request fails
Hope it helps