发送PHP GET请求到Sockets.IO

我试图从PHP(CLI)发送一个GET或POST请求到一个Node.js / Sockets.IO应用程序,只使用基本的cURL。 这是我到目前为止,我可以看到响应进入node.js(从cli),但不能得到任何更远。 目前我只想把参数发送到控制台。 (我可以在以后扩展)

任何帮助将是伟大的!

(仅供参考:我从php源代码看了这个, Socket.io ,但是需要更多的帮助,精确的代码会很棒)

PHP

$qry_str = "?msg_from_php=This_is_a_test123&y=20"; $ServerAddress = 'http://10.1.1.69/socket.io/1/websocket/TICWI50sbew59XRE-O'; $ServerPort = '4000'; $TimeOut = 20; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ServerAddress. $qry_str); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); curl_setopt($ch, CURLOPT_PORT, $ServerPort); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $TimeOut); curl_setopt($ch, CURLOPT_TIMEOUT, '3'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); // not sure if it should be in an array //$data = array('msg_from_php' => 'simple message!'); //curl_setopt($ch, CURLOPT_POSTFIELDS, $data); $content = trim(curl_exec($ch)); curl_close($ch); echo " Sent! Content: $content \r\n"; 

Node.js的

 var express = require('express'), http = require('http'); var app = express(); var server = http.createServer(app); var io = require('socket.io').listen(server); io.configure('production', function(){ io.enable('browser client minification'); io.enable('browser client etag'); io.enable('browser client gzip'); io.set('log level', 1); io.set('transports', ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']); io.set("polling duration", 30); }); server.listen(4000); // 80,443, 843, 4000, 4001 io.sockets.on('connection', function (socket) { socket.on('msg_from_php, function (data) { console.log(data); }); }); 

你正试图build立一个普通的HTTP连接到一个socket.io服务器,但是socket.io不会说普通的HTTP; 它至less使用一个专门的握手协议,如果它使用websocket传输它根本不会使用HTTP。 AFAIK没有一个socket.io客户端的PHP实现。

幸运的是,看起来您的PHP应用程序需要按自己的条件发送给您的节点应用程序,而不是相反,所以您只需使用express来定义一对路由来实现RESTful接口; 然后您的PHP应用程序可以使用cURL来POST到相应的路由对应的URL。