简体   繁体   中英

From node.js request JSON file to PHP

From node.js I would like to get JSON data from a PHP file. I thought about using the request package, but I am not sure about what to write in the PHP file in order to send the JSON back to node:

Node:

var request = require('request');
request
        .get('http://IP/File.php')
        .on('response', function(response) {
            data = JSON.parse(data from the php file);
        });

PHP:

$data = array('message' => 'HeLLO');
$json = json_encode($data);
when executing this, send $json to node

you need to print a response from your .PHP file:

$data = array('message' => 'HeLLO');
$json = json_encode($data);
print_r($json);

javascript:

var request = require('request');
request('http://IP/File.php', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    data = JSON.parse(body)[0];
  }
})

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM