2

I have a websocket server on AWS, currently I am connecting to this using JavaScript websockets. Is there a way to connect to the websocket server using PHP (preferably with no libraries). all I have to do is send a simple message to the websocket server, and then close connection. Hopefully with the message sent, the JavaScript on a second page listening to the websocket will pick up this message.

Any help would be much appreciated.

1
  • Is there a way to connect to the websocket server using PHP... yes .... preferably with no libraries...again yes but why put yourself through that pain? Don't re-invent the wheel. It's not a super-simple process, so it's far better to use some ready-made, tested code. Follow this link for more information. P.S. People occasionally come here and ask how to do something fairly standard "without libraries", but their reasons for avoiding libraries rarely make any sense when scrutinised. Everyone uses libraries, just get on with it. Commented Apr 29, 2021 at 9:53

2 Answers 2

2

I had a similar task as you described in the topic. Basically I needed a PHP client to connect with my Socket.IO v4.4.1 server (Node.js with SSL/TLS). And here is a working code example:

// random 7 chars digit/alphabet for "t" param
$random = substr(md5(mt_rand()), 0, 7);
$socket_server='https://my-nodejs.server/socket.io/?EIO=4&transport=polling&t='.$random;

// create curl resource
$ch = curl_init();

//N°1 GET request
curl_setopt_array(
    $ch,
    [
        CURLOPT_URL => $socket_server,
        CURLOPT_RETURNTRANSFER => TRUE,
        // since it is TRUE by default we should disable it
        // on our localhost, but from real https server it will work with TRUE
        CURLOPT_SSL_VERIFYPEER => FALSE
    ]);

// $output contains the output string
$output = curl_exec($ch);

$output=substr($output, 1);

echo $socket_server;
// server response in my case was looking like that:
// '{"sid":"4liJK2jWEwmTykwjAAAR","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":20000,"maxPayload":1000000}'
var_dump($output);

$decod=json_decode($output);
// setting ping Interval accordingly with server response
$pingInterval = $decod->pingInterval;


//N°2 POST request
$socket_server_with_sid = $socket_server.'&sid='.$decod->sid;

curl_setopt_array(
    $ch,
    [
        CURLOPT_URL => $socket_server_with_sid,
        CURLOPT_POST => TRUE,
        CURLOPT_TIMEOUT_MS => $pingInterval,
        // 4           => Engine.IO "message" packet type
        // 0           => Socket.IO "CONNECT" packet type
        CURLOPT_POSTFIELDS => '40'
    ]);

$output = curl_exec($ch);

// ok
var_dump($output);

// Pervious 2 requests are called "hand shake" now we can send a message
// N°3 socket.emit
if ($output==='ok')  {
    curl_setopt_array(
        $ch,
        [
            CURLOPT_URL => $socket_server_with_sid,
            CURLOPT_TIMEOUT_MS => $pingInterval,
            CURLOPT_POST => TRUE,
            // 4           => Engine.IO "message" packet type
            // 2           => Engine.IO "EVENT" packet type
            CURLOPT_POSTFIELDS => '42["chat message","0devhost message 10"]'
        ]);

    $output = curl_exec($ch);
    // ok
    echo $output.'<br/>';
    echo $socket_server_with_sid;
}

// close curl resource to free up system resources
curl_close($ch);

If you execute the code on your PHP server with https protocol, you can just comment out CURLOPT_SSL_VERIFYPEER param.

I simulated these requests with the POSTMAN, and it worked as well (don't forget to make all 3 request in a short time frame "ping interval", otherwise it will be automatically closed): POSTMAN node client requests simulation

Links that helped me to come up to the solution and could be helpful for you as well:

  1. https://stackoverflow.com/a/65893724/3936149
  2. https://stackoverflow.com/a/65572463/3936149
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your example helped to make a connection with WS server.
1

Connecting to a web socket server from php, requires to perform a handshake and to send your data in frames as defined by RFC6455. You can go to https://github.com/napengam/phpWebSocketServer and look into directory phpClient, there I have implemented a class to do all the above. This works with my web socket server, but it would be interesting to see if it will work with another web socket server. You might need to modify method writeWait, to not wait for an answer from the server. Good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.