1

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

1 Answer 1

1

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];
  }
})
Sign up to request clarification or add additional context in comments.

2 Comments

And how can I manage the json data from php in the node file?
@Nitsan:I'm getting below error using above method error:null response:[object Object] body:Cannot GET /file.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.