Is there a way to send any kind of request from the PHP script to the Node.js script?
For example I have this directory:
scripts
|_sender.php
|_receiver.js
I want to send some data from php script and read it with node.js script to execute some action.
How is this done properly?
It depends where js will read it incoming data
If it is a server, start it with node receiver.js
then send from your php to http://local host/.... Whatever your server is listening on
Or you can dump your php output into a file and read it by the receiver after
You should provide more informations to get a better answer
The easiest way I use is to pass your PHP data to node using HTTP post or get, here is my code to send data from PHP to the node.
// Node Side
express = require('express');
bodyParser = require('body-parser');
express.use(bodyParser.json());
express.post('/get_php_data', function (req, res) {
// php array will be here in this variable
var data = req.body.data;
res.send(' Done ');
});
httpPost('NODE_URL:2200/get_php_data', array('data' => 'some data'));
// PHP Side
function httpPost($url,$params)
{
$postData = http_build_query($params);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.