CYRUS
CYRUS

Reputation: 41

Unable to connect to websocket using wss:// in php

first of all I want to apologize if there is some mistakes in my post I'm not a native english (french). So I'm facing a problem trying to connect myself to a websocket with a wss:// domain. The domain is this one : wss://engine.coss.io/api/v1/ws/v1/ht/{ETH_BTC}. the last part of the URL is a king of query string you can find the detail of this at : https://api.coss.io/v1/spec

First of all I tried to connect to this adresse through chrome but I get the error "ERR_DISALLOWED_URL_SCHEME" and I found that is was related to some sort of missing certificates. But my goal was to make it work with php so I tried several ways to connect to this adress but nothing worked this is one of the way I tried to connect to it :

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
echo $socket;
echo socket_connect($socket ,"35.198.217.124",443)?"true":"false";


$in = "GET / HTTP/1.1\r\n";
$in .= "Host: www.example.com\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';

echo "Sending HTTP GET request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";

echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}

Thank you for any help :) Happy New Year !

EDIT :

After multiples try I get to this situation where I have two pieces of code on getting 200 responce on the root of the machine but definitely not what I want and the second one which get 400 error bad request the two pieces of code are right here :

the 200 response on the root (note that if I add /api to the path I get 404 error):

$contextOptions = array(
    'ssl' => array(
        "verify_peer" => false,
        "verify_peer_name" => false
    )
);
$context = stream_context_create($contextOptions);

$fp = stream_socket_client("ssl://engine.coss.io:443", $errstr, $errno, 30, STREAM_CLIENT_CONNECT, $context);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    echo "true\n";

    fwrite($fp, "GET / HTTP/1.1\r\n" .
        "Host: www.engine.coss.io\r\n" .
        "Accept: */*\r\n" .
        "Connection: Upgrade\r\n" .
        "Upgrade: websocket\r\n\r\n");
    while (!feof($fp)) {
        echo fgets($fp, 1024);
    }
    fclose($fp);
}

And this one gives me a 400 bad Request :

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
echo $socket;
echo socket_connect($socket ,"35.198.217.124",443)?"true":"false";


$in = "GET /api/v1/ws/v1/ht/{ETH_BTC} HTTP/1.1\r\n";
$in .= "Host: https://www.engine.coss.io\r\nAccept: */*\r\nConnection: Upgrade\r\nUpgrade: websocket\r\n\r\n";
//$in .= "Connection: Close\r\n\r\n";
$out = '';

echo "Sending HTTP GET request...";
socket_write($socket, $in, strlen($in));
echo "OK.\n";

echo "Reading response:\n\n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}

EDIT 2 :

So I email the support of the website they told me that there was a mistake on the URL, the right one is : wss://engine.coss.io/ws/v1/ht/{ETH_BTC}

This seems to work with this code because I get a 101 successful handshake but the problem now is that the script ends once the handshake is successful so I never get the informations I want, maybe there is a way to keep the script listening throught the websocket ? I didn't find anything in the documentation ...

code :

$contextOptions = array(
    'ssl' => array(
        "verify_peer"=>false,
        "verify_peer_name"=>false
    )
);
$context = stream_context_create($contextOptions);

$fp =stream_socket_client("ssl://engine.coss.io:443/ws/v1/ht/{COSS_ETH}",$errstr,$errno,30,STREAM_CLIENT_CONNECT,$context);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
echo "true\n";



fwrite($fp, "GET /ws/v1/ht/{COSS_ETH} HTTP/1.1\r\nHost: engine.coss.io\r\nAccept: */*\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n");
    // Notez que si ici je rajoute au path /api/v1/ws/v1/ht/{ETH_BTC} j'obtiens une erreur 404 not found ...

    while (!feof($fp)) {

        echo fgets($fp, 2024);

    }
}

Upvotes: 2

Views: 8677

Answers (3)

Chisom Maxwell
Chisom Maxwell

Reputation: 197

You should try using textalk/websocket package. after installing with composer

require("vendor/autoload.php")
use WebSocket\Client;

 $client = new Client("wss://engine.coss.io/api/v1/ws/v1/ht/{ETH_BTC}");
        if(isset($client)){
            while(true){
                try{
                    $message = $client->receive();
                    Log::alert($message);
                }
                catch(ConnectionException $e){
                    Log::error("failed to fetch price updates ".$e->getMessage());
                    break;
                }
               

            }
        }

Upvotes: 0

EvE
EvE

Reputation: 750

I've made your second code block working (the one with stream_context_create()). The code below retrieves the exchange rate between ETH and BTC. I don't think you need to implement wss:// anymore, because their api seems also be available on https://

$contextOptions = array(
    'ssl' => array(
        "verify_peer" => false,
        "verify_peer_name" => false
    )
);
$context = stream_context_create($contextOptions);

$fp = stream_socket_client("ssl://engine.coss.io:443", $errstr, $errno, 30, STREAM_CLIENT_CONNECT, $context);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    echo "true\n";

    fwrite($fp, "GET /api/v1/dp?symbol=ETH_BTC  HTTP/1.1\r\n" .
        "Host: engine.coss.io\r\n" .
        "Accept: */*\r\n" .
        "Connection: Upgrade\r\n" .
        "Upgrade: websocket\r\n\r\n");
    while (!feof($fp)) {
        echo fgets($fp, 1024);
    }
    fclose($fp);
}

returns:

{"symbol":"ETH_BTC","asks":[["0.03698000","0.12000000"],["0.03705000","0.12000000"],["0.03715000","0.12000000"], .....

Upvotes: 0

EvE
EvE

Reputation: 750

The 'wss://' stream is not supported by PHP by default, but you can implement your own 'wss://' StreamWrapper class.

Here's an example. Changing 'var' into 'wss' in stream_wrapper_register() will get you started.

http://php.net/manual/en/stream.streamwrapper.example-1.php

Here are all the methods you can implement in your StreamWrapper class, but you probably won't need them all.

http://php.net/manual/en/class.streamwrapper.php

Good luck!

Upvotes: 2

Related Questions