从node.js请求JSON文件到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]; } })