Sending data from php to nodejs server
Is there any way to send string or Json data from php to nodeJS server.
My project use php(Nginx server serves it). And nodeJs server(8080 port, NowJS) is running as background for realtime Pub/Sub update.
When a user write a message, database(Mysql) also is updatd using php. And the change have to send to nodeJS(NowJS, 8080 port) server for realtime updating.
Realtime Update Mechanism :开发者_Go百科 user writes -> php save it in mysql db -> php send info to nodeJS -> nodeJS send the change all subscribers -> others can notice it in realtime.
So my question is, Is there any way to send the changes from php to NodeJS server? I am a nodeJS newbie. Any comments welcomes~ ^^;
Any reason you can't use curl?
$ch = curl_init('http://yournodehost/path');
curl_exec($ch);
Depending on the requirements of your request you can set the appropriate curl options.
- http://us2.php.net/curl_init
- http://us2.php.net/curl_setopt
Node.js is pretty flexible, but if you've implemented an http server with it, then your PHP can use file_get_contents to fetch a URL. http://php.net/manual/en/function.file-get-contents.php . This is only for a GET method, you'll have to encode your json appropriately...
As a side note, you might want to have your server modify the database, instead of mixing the functionality half in the PHP and half in the node.js server. That might help keep all the related work in one place, possibly easier to maintain, debug, expand, and so on.
I wouldn't recommend you the solutions so far used.
If I was going one way, I would use AMQP (npm install amqp). https://github.com/ry/node-amqp
It's not complete, but it works pretty decently for integration between different techonologies, as it follows the Message Queueing pattern. PHP has a PECL extension for using AMQP: http://mx.php.net/manual/en/book.amqp.php
Both can communicate to each other by using it. It is not THAT simple to use, but it works really well and can lead to scalable applications if done right.
Since NowJS uses Socket.io, and Socket.io provides a plain HTTP transport called htmlfile (as opposed to WebSockets), it might be possible to make an HTTP POST request to the server by using curl or pecl_http or file_get_contents. However, NowJS has its own conventions for the data format. Instead of re-implementing this format in PHP I would suggest an alternate approach:
Create a third HTTP server in node (in the same process as your NowJS server) that listens on a separate port, such as 8081. This HTTP server is the web service for your application. Your PHP application makes a request to the third web server. The third webserver parses these requests and triggers events through NowJS (since accessing that scope is trivial with node).
It would look something like this:
var http = require('http');
var nowServer = http.createServer();
nowServer.listen(8080, "127.0.0.1");
var everyone = require("now").initialize(nowServer);
var apiServer = http.createServer(function (req, res) {
switch (req.url) {
case '/do_something':
everyone.now.doSomething();
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end();
break;
default:
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end('No such service\n');
break;
}
});
apiServer.listen(8081, "127.0.0.1");
Double warning: This is completely untested and I have never used NowJS.
精彩评论