1

Im trying to send a GET or POST request from PHP (CLI), to a Node.js/Sockets.IO application, using only basic cURL. This is what i have so far, i can see the response coming in to node.js (from the cli), but can not get any farther. Currently i only want the parameters sent to the console. (I can expand on that later)

Any help would be great!

(FYI: I did look at this, Socket.io from php source, but need more help. Exact code would be great)

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);
    });
});
Community
  • 1
  • 1
CGray
  • 490
  • 5
  • 8

1 Answers1

2

You're trying to make an ordinary HTTP connection to a socket.io server, but socket.io doesn't speak plain HTTP; it uses at the very least a specialized handshaking protocol, and if it uses websocket transport it won't be using HTTP at all. AFAIK there's no PHP implementation of a socket.io client.

Fortunately, it looks like your PHP application needs to send to your node application on its own terms, not the other way around, so all you need to do is use express to define a couple routes to implement a RESTful interface; your PHP app can then use cURL to POST to the URL corresponding to the appropriate route.

ebohlman
  • 14,795
  • 5
  • 33
  • 35
  • Thats exactly right, on what im trying to do. Could you provide me a link where i can get some info on doing this, or even better, supply some sample node code for it? Thank you so much – CGray Sep 26 '12 at 17:55