未加星标

Sending data from php to nodejs server

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二04 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

Is there any way to send string or Json data fromphp 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 comments welcomes~ ^^;

Problem courtesy of: Jinbom Heo

Solution

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

Solution courtesy of: jmathai

Discussion

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.

Discussion courtesy of: igorw

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.

Discussion courtesy of: Mamsaac

Node.jsis 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.

Discussion courtesy of: david van brink

This recipe can be found in it's original form on Stack Over Flow .

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: PHPWebSocketNode.jsNginxHead
tags: php,http,NowJS,curl,your
分页:12
转载请注明
本文标题:Sending data from php to nodejs server
本站链接:http://www.codesec.net/view/561331.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(52)