admin管理员组文章数量:1277895
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 : 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 ments weles~ ^^;
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 : 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 ments weles~ ^^;
Share Improve this question asked Apr 3, 2011 at 16:04 Jinbom HeoJinbom Heo 7,40014 gold badges55 silver badges60 bronze badges4 Answers
Reset to default 5Any 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.
- https://www.php/curl_init
- https://www.php/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/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 remend you the solutions so far used.
If I was going one way, I would use AMQP (npm install amqp). https://github./ry/node-amqp
It's not plete, 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/manual/en/book.amqp.php
Both can municate 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 pletely untested and I have never used NowJS.
本文标签: javascriptSending data from php to nodejs serverStack Overflow
版权声明:本文标题:javascript - Sending data from php to nodejs server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741274598a2369654.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论