Communication between PHP and NodeJS

Viewed 101

I was wondering what would be the best way to communicate between my PHP script and a NodeJS service running on localhost (not only for security purposes).

I thought of just getting the NodeJS response I need with a file_get_contents('http://localhost:8080/****'), would it be the best way ?

1 Answers

file_get_contents is the best choice if you are just sharing some data when needed, (and it's a zero-cost implementation)

But if you want a more live and "native" communication to stream your data without limits you could implement TCP socket communication between the two (but it's an overkill in most cases)

Or when you don't want to use network for transfer (i.e if you are moving large data) you could output it to a file and request NodeJS to read it with

file_put_contents($tmp_file, $large_data_for_node);
file_get_contents('http://localhost:8080/****?read='.$tmp_file);
unlink($tmp_file);
Related