从PHP发送消息到Node.js [英] Sending messages from PHP to Node.js

查看:88
本文介绍了从PHP发送消息到Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将消息从php发送到node.js?我有一个运行php和node.js的Linux服务器.

How to send messages from php to node.js? I have a linux server running php and node.js.

当用户完成交易(通过php)时,我想将消息从php发送到node.js.然后,节点将通过套接字连接更新客户端.

When a user completes a transaction (via php), I'd like send a message from php to node.js. Node will then update the client via a socket connection.

在不降低node.js性能的情况下,从php向node.js发送少量数据的好方法是什么?

What's a good way to send a small amount of data from php to node.js without defeating the performance of node.js?

推荐答案

与其他任何客户端一样,建议似乎是通过HTTP接口与节点通信.您可以使用php中的cURL通过HTTP与node对话

The suggestion seems to be to talk to node through the HTTP interface, just as any other client does. You can talk to node via HTTP using cURL in php

请参阅: http://groups.google.com/group/socket_io /browse_thread/thread/74a76896d2b72ccc/216933a076ac2595?pli = 1

特别是,请参阅Matt Pardee的这篇文章

In particular, see this post from Matt Pardee

我也面临着类似的问题,希望让用户了解新的 便笺上添加了错误说明,以及可能确实会发出类似通知 只能从PHP有效地发送到我的Node服务器.我做了什么 如下(如果这在所有情况下都出现乱码和未格式化,则表示歉意 发送(如果有的话),我很乐意将代码粘贴到其他地方): 首先,您需要使用PHP中的cURL.我为我写了一个函数 像这样的课程:

I faced a similar problem with wanting to keep users informed of a new note added on to a bug, and similar notifications that could really only be effectively sent from PHP to my Node server. What I did follows (apologies if this gets all garbled and unformatted in sending, if it does, I'd be happy to paste the code somewhere else): First, you'll need to use cURL from PHP. I wrote a function for my class like this:

function notifyNode($type, $project_id, $from_user, $data) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'http://127.0.0.1');

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
    curl_setopt($ch, CURLOPT_PORT, 8001);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);

    curl_setopt($ch, CURLOPT_POST, true);

    $pf = array('f' => $type, 'pid' => $project_id, 'user_from' => $from_user, 
             'data' => array());

    foreach($data as $k => $v) {
        $pf['data'][$k] = $v;
    }

    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($pf));

    curl_exec($ch);
    curl_close($ch);
}

您会注意到,由于 PHP和NodeJS都在那儿运行,您的行程可能会有所不同.港口 我将此代码设置为8001(这是我的Node服务器的端口 正在运行,并且socket.io服务器连接到的端口).这 发送一个HTTP POST请求,其中的post字段已编码.这是所有的了 非常标准的cURL内容.

You'll notice that I send the cURL request on the same server since both PHP and NodeJS are running there, your mileage may vary. The port I set this code to connect to is 8001 (this is the port my Node server is running on, and the port the socket.io server connects to). This sends a HTTP POST request with the post field encoded. This is all pretty standard cURL stuff.

在您的Node应用程序中,您可能会有类似的内容:

In your Node app you probably have something like:

var server = http.createServer(function(req, res) {});
server.listen(8001);
var io = io.listen(server, { transports: ['websocket', 'flashsocket', 'xhr-polling'] });

...

在这里我们要做的是在http.createServer部分上进行扩展,以 侦听来自本地主机("127.0.0.1")的连接.这 然后,createServer代码变为:

well what we'll do here is expand on the http.createServer part, to listen for connections coming from our local host ("127.0.0.1"). The createServer code then becomes:

var server = http.createServer(function(req, res) {
    // Check for notices from PHP
    if(res.socket.remoteAddress == '127.0.0.1') {
        if(req.method == 'POST') {
            // The server is trying to send us an activity message

            var form = new formidable.IncomingForm();
            form.parse(req, function(err, fields, files) {

                res.writeHead(200, [[ "Content-Type", "text/plain"]
                        , ["Content-Length", 0]
                        ]);
                res.write('');
                res.end();

                //sys.puts(sys.inspect({fields: fields}, true, 4));

                handleServerNotice(fields);                
            });
        }
    }
});

从那里可以实现handleServerNotice函数.

From there you can implement your handleServerNotice function..

function handleServerNotice(data) {
        ...
}

等.我已经有一段时间没有对此进行测试了,实际上该代码块 在我的节点服务器上被注释掉了,所以希望我粘贴到这里 可行-一般而言,此概念已得到证实,我认为它将适用于 你.无论如何只是想确保您知道已经几个月了,所以 我不确定为什么要发表评论.我写的代码花了一个 很少的研究-例如在cURL中设置'Expect:'标头-然后我 终于奏效时,我非常兴奋.让我知道你是否需要 其他帮助.

etc etc. I haven't tested this in a while, and in fact that code block was commented out on my node server, so I hope what I've pasted here works - in general this concept is proven and I think it'll work for you. Anyway just wanted to be sure you knew it's been a few months so I'm not sure exactly why I commented out. The code I wrote took a little research -- like setting the 'Expect:' header in cURL -- and I was pretty excited when it finally worked. Let me know if you need any additional help.

最好

马特·帕迪(Matt Pardee)

Matt Pardee

这篇关于从PHP发送消息到Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆