11

I can't find any information about setting up a wss connection in PHP. I have no problem to be connected throw ws. I'm using this pretty great library to do this by the way: https://github.com/albeva/php-websocket

But my website use https and I need a WebSocket Secure connection to avoid Firefox to complain about the fact that the connection is insecure.

Thanks in advance for your help.


Edit

Here is the code used by the library to start the socket connection:

$master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!is_resource($master)) {
    $this->throwSocketError();
}

// set options
if (!socket_set_option($master, SOL_SOCKET, SO_REUSEADDR, 1)) {
    $this->throwSocketError();
}

// bind
if (!socket_bind($master, $this->host, $this->port)) {
    $this->throwSocketError();
}

// listen for connections
if (!socket_listen($master, SOMAXCONN)) {
    $this->throwSocketError();
}

I believe I need to change this code but I can't find how.

3 Answers 3

4

Did you try stream_socket_server ?

<?php
$socket = stream_socket_server("ssl://0.0.0.0:".$port, $errno, $errstr);
if (!$socket) {
  echo "$errstr ($errno)<br />\n";
} else {
  while ($conn = stream_socket_accept($socket)) {
    fwrite($conn, 'some data...\n');
    fclose($conn);
  }
  fclose($socket);
}
?>
Sign up to request clarification or add additional context in comments.

Comments

2
+25

Here is an example (and link), which was modified slightly, and found from another SO post. Link to other post

And here is a link to the stream_context_create php function definition. function definition

try 
{ 
    $localCertificateFilespec = $connection['localCertificateFilespec']; 
    $localCertificatePassphrase = $connection['localCertificatePassphrase']; 

    $sslOptions = array( 
        'ssl' => array( 
            'local_cert' => $localCertificateFilespec, 
            'passphrase' => $localCertificatePassphrase, 
            'allow_self_signed' => true, 
            'verify_peer' => false 
        ) 
    ); 
    $sslContext = stream_context_create($sslOptions); 

    $clientArguments = array( 
        'stream_context' => $sslContext, 
        'local_cert' => $localCertificateFilespec, 
        'passphrase' => $localCertificatePassphrase, 
        'trace' => true, 
        'exceptions' => true, 
        'encoding' => 'UTF-8', 
        'soap_version' => SOAP_1_1 
    ); 

    $oClient = new WSSoapClient($connection['wsdlFilespec'], $clientArguments); 
    $oClient->__setUsernameToken($connection['username'], $connection['password']); 

    return $oClient->__soapCall($operation, $request); 
} 

However, at the very bottom of the linked SO post, you will find an answer from "Sam". I am needing to do this same thing in the next few weeks, so I plan on using Sams method... as I am more of a fan of CURL within PHP.

1 Comment

I've updated my post to show the start function (which use the php socket extension) of the library I'm using. I had already read the post you're pointing out but wasn't able to make it work with this code.
2

Your best shot on PHP is currently to use Ratchet. The best way to setup an wss connection, is to put your websocket server behind Nginx or HAProxy which will handle all the SSL stuff for you. This is explained here with HAProxy. Note that you can do the same with Nginx since it also now support WebSocket.

1 Comment

I find stunnel a simple way to SSLize insecure services.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.