从node.js向PHP请求JSON文件


From node.js request JSON file to PHP

从node.js我想从PHP文件中获取JSON数据。我考虑过使用请求包,但我不确定在PHP文件中写什么才能将JSON发送回节点:

节点:

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

您需要从.PHP文件打印一个响应:

$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];
  }
})