简体   繁体   English

将数据从php发送到nodejs服务器

[英]Sending data from php to nodejs server

Is there any way to send string or Json data from php to nodeJS server. 有没有办法从php发送字符串或Json数据到nodeJS服务器。

My project use php(Nginx server serves it). 我的项目使用php(Nginx服务器提供服务)。 And nodeJs server(8080 port, NowJS) is running as background for realtime Pub/Sub update. nodeJs服务器(8080端口,NowJS)作为实时发布/订阅更新的后台运行。

When a user write a message, database(Mysql) also is updatd using php. 当用户编写消息时,数据库(Mysql)也会使用php进行更新。 And the change have to send to nodeJS(NowJS, 8080 port) server for realtime updating. 并且必须将更改发送到nodeJS(NowJS,8080端口)服务器以进行实时更新。

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. 实时更新机制:用户写入 - > php将其保存在mysql db - > php发送信息到nodeJS - > nodeJS发送更改所有订阅者 - >其他人可以实时注意到它。

So my question is, Is there any way to send the changes from php to NodeJS server? 所以我的问题是,有没有办法将更改从php发送到NodeJS服务器? I am a nodeJS newbie. 我是nodeJS新手。 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. 根据您的要求,您可以设置适当的卷曲选项。

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. Node.js非常灵活,但是如果你用它实现了一个http服务器,那么你的PHP可以使用file_get_contents来获取URL。 http://php.net/manual/en/function.file-get-contents.php . 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... 这只适用于GET方法,你必须适当地编码你的json ...

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. 作为旁注,您可能希望让服务器修改数据库,而不是将PHP中的一半功能和node.js服务器中的一半混合使用。 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). 如果我走一条路,我会使用AMQP(npm install amqp)。 https://github.com/ry/node-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 PHP有一个使用AMQP的PECL扩展: 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. 由于NowJS使用Socket.io ,而Socket.io提供了一个名为htmlfile的普通HTTP传输(而不是WebSockets),因此可以使用curl或pecl_http或file_get_contents向服务器发出HTTP POST请求。 However, NowJS has its own conventions for the data format. 但是,NowJS有自己的数据格式约定。 Instead of re-implementing this format in PHP I would suggest an alternate approach: 我没有在PHP中重新实现这种格式,而是建议另一种方法:

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. 在节点(与NowJS服务器处于同一进程中)中创建第三个HTTP服务器,该服务器侦听单独的端口,例如8081.此HTTP服务器是应用程序的Web服务。 Your PHP application makes a request to the third web server. 您的PHP应用程序向第三个Web服务器发出请求。 The third webserver parses these requests and triggers events through NowJS (since accessing that scope is trivial with node). 第三个Web服务器解析这些请求并通过NowJS触发事件(因为访问该范围对于节点来说是微不足道的)。

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. 双重警告:这是完全未经测试的,我从未使用过NowJS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM