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












0















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.1rn";
$in .= "Host: www.example.comrn";
$in .= "Connection: Closernrn";
$out = '';

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

echo "Reading response:nn";
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 "truen";

fwrite($fp, "GET / HTTP/1.1rn" .
"Host: www.engine.coss.iorn" .
"Accept: */*rn" .
"Connection: Upgradern" .
"Upgrade: websocketrnrn");
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.1rn";
$in .= "Host: https://www.engine.coss.iornAccept: */*rnConnection: UpgradernUpgrade: websocketrnrn";
//$in .= "Connection: Closernrn";
$out = '';

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

echo "Reading response:nn";
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 "truen";



fwrite($fp, "GET /ws/v1/ht/{COSS_ETH} HTTP/1.1rnHost: engine.coss.iornAccept: */*rnConnection: UpgradernUpgrade: websocketrnSec-WebSocket-Version: 13rnSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==rnrn");
// 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);

}
}









share|improve this question

























  • Possible duplicate of WebSocket client in PHP?

    – Binar Web
    Dec 31 '18 at 17:25











  • I saw this post but was not helpfull, because the problem come from the adress, and php 7 have the tools needed to connect to a websocket no need to a library as they say in your post ...

    – CYRUS
    Dec 31 '18 at 17:41











  • seeing your question, this is not how the websockets work. perhaps you need to read about websockets first.

    – Binar Web
    Jan 2 at 14:04











  • @Binar Web I saw your link but I couldn't learn more about websockets in php, the thing is that I'm close to get the connection because the hanshake is successful but afterwards it closes the connection, I've read a lot in the rfc documentation but nothing helped me if you can i'll be very gratefull

    – CYRUS
    Jan 2 at 14:42
















0















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.1rn";
$in .= "Host: www.example.comrn";
$in .= "Connection: Closernrn";
$out = '';

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

echo "Reading response:nn";
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 "truen";

fwrite($fp, "GET / HTTP/1.1rn" .
"Host: www.engine.coss.iorn" .
"Accept: */*rn" .
"Connection: Upgradern" .
"Upgrade: websocketrnrn");
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.1rn";
$in .= "Host: https://www.engine.coss.iornAccept: */*rnConnection: UpgradernUpgrade: websocketrnrn";
//$in .= "Connection: Closernrn";
$out = '';

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

echo "Reading response:nn";
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 "truen";



fwrite($fp, "GET /ws/v1/ht/{COSS_ETH} HTTP/1.1rnHost: engine.coss.iornAccept: */*rnConnection: UpgradernUpgrade: websocketrnSec-WebSocket-Version: 13rnSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==rnrn");
// 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);

}
}









share|improve this question

























  • Possible duplicate of WebSocket client in PHP?

    – Binar Web
    Dec 31 '18 at 17:25











  • I saw this post but was not helpfull, because the problem come from the adress, and php 7 have the tools needed to connect to a websocket no need to a library as they say in your post ...

    – CYRUS
    Dec 31 '18 at 17:41











  • seeing your question, this is not how the websockets work. perhaps you need to read about websockets first.

    – Binar Web
    Jan 2 at 14:04











  • @Binar Web I saw your link but I couldn't learn more about websockets in php, the thing is that I'm close to get the connection because the hanshake is successful but afterwards it closes the connection, I've read a lot in the rfc documentation but nothing helped me if you can i'll be very gratefull

    – CYRUS
    Jan 2 at 14:42














0












0








0








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.1rn";
$in .= "Host: www.example.comrn";
$in .= "Connection: Closernrn";
$out = '';

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

echo "Reading response:nn";
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 "truen";

fwrite($fp, "GET / HTTP/1.1rn" .
"Host: www.engine.coss.iorn" .
"Accept: */*rn" .
"Connection: Upgradern" .
"Upgrade: websocketrnrn");
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.1rn";
$in .= "Host: https://www.engine.coss.iornAccept: */*rnConnection: UpgradernUpgrade: websocketrnrn";
//$in .= "Connection: Closernrn";
$out = '';

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

echo "Reading response:nn";
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 "truen";



fwrite($fp, "GET /ws/v1/ht/{COSS_ETH} HTTP/1.1rnHost: engine.coss.iornAccept: */*rnConnection: UpgradernUpgrade: websocketrnSec-WebSocket-Version: 13rnSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==rnrn");
// 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);

}
}









share|improve this question
















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.1rn";
$in .= "Host: www.example.comrn";
$in .= "Connection: Closernrn";
$out = '';

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

echo "Reading response:nn";
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 "truen";

fwrite($fp, "GET / HTTP/1.1rn" .
"Host: www.engine.coss.iorn" .
"Accept: */*rn" .
"Connection: Upgradern" .
"Upgrade: websocketrnrn");
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.1rn";
$in .= "Host: https://www.engine.coss.iornAccept: */*rnConnection: UpgradernUpgrade: websocketrnrn";
//$in .= "Connection: Closernrn";
$out = '';

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

echo "Reading response:nn";
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 "truen";



fwrite($fp, "GET /ws/v1/ht/{COSS_ETH} HTTP/1.1rnHost: engine.coss.iornAccept: */*rnConnection: UpgradernUpgrade: websocketrnSec-WebSocket-Version: 13rnSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==rnrn");
// 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);

}
}






php sockets http websocket get






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 11:38







CYRUS

















asked Dec 31 '18 at 16:42









CYRUSCYRUS

94




94













  • Possible duplicate of WebSocket client in PHP?

    – Binar Web
    Dec 31 '18 at 17:25











  • I saw this post but was not helpfull, because the problem come from the adress, and php 7 have the tools needed to connect to a websocket no need to a library as they say in your post ...

    – CYRUS
    Dec 31 '18 at 17:41











  • seeing your question, this is not how the websockets work. perhaps you need to read about websockets first.

    – Binar Web
    Jan 2 at 14:04











  • @Binar Web I saw your link but I couldn't learn more about websockets in php, the thing is that I'm close to get the connection because the hanshake is successful but afterwards it closes the connection, I've read a lot in the rfc documentation but nothing helped me if you can i'll be very gratefull

    – CYRUS
    Jan 2 at 14:42



















  • Possible duplicate of WebSocket client in PHP?

    – Binar Web
    Dec 31 '18 at 17:25











  • I saw this post but was not helpfull, because the problem come from the adress, and php 7 have the tools needed to connect to a websocket no need to a library as they say in your post ...

    – CYRUS
    Dec 31 '18 at 17:41











  • seeing your question, this is not how the websockets work. perhaps you need to read about websockets first.

    – Binar Web
    Jan 2 at 14:04











  • @Binar Web I saw your link but I couldn't learn more about websockets in php, the thing is that I'm close to get the connection because the hanshake is successful but afterwards it closes the connection, I've read a lot in the rfc documentation but nothing helped me if you can i'll be very gratefull

    – CYRUS
    Jan 2 at 14:42

















Possible duplicate of WebSocket client in PHP?

– Binar Web
Dec 31 '18 at 17:25





Possible duplicate of WebSocket client in PHP?

– Binar Web
Dec 31 '18 at 17:25













I saw this post but was not helpfull, because the problem come from the adress, and php 7 have the tools needed to connect to a websocket no need to a library as they say in your post ...

– CYRUS
Dec 31 '18 at 17:41





I saw this post but was not helpfull, because the problem come from the adress, and php 7 have the tools needed to connect to a websocket no need to a library as they say in your post ...

– CYRUS
Dec 31 '18 at 17:41













seeing your question, this is not how the websockets work. perhaps you need to read about websockets first.

– Binar Web
Jan 2 at 14:04





seeing your question, this is not how the websockets work. perhaps you need to read about websockets first.

– Binar Web
Jan 2 at 14:04













@Binar Web I saw your link but I couldn't learn more about websockets in php, the thing is that I'm close to get the connection because the hanshake is successful but afterwards it closes the connection, I've read a lot in the rfc documentation but nothing helped me if you can i'll be very gratefull

– CYRUS
Jan 2 at 14:42





@Binar Web I saw your link but I couldn't learn more about websockets in php, the thing is that I'm close to get the connection because the hanshake is successful but afterwards it closes the connection, I've read a lot in the rfc documentation but nothing helped me if you can i'll be very gratefull

– CYRUS
Jan 2 at 14:42












2 Answers
2






active

oldest

votes


















0














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!






share|improve this answer
























  • Thanks a lot for your answer ! I will look at your link right after writing my answer. But I was wondering : I got the ip adress of the web socket then I made a line in php : echo socket_connect($socket ,"35.198.217.124",443)?"true":"non" and i got true but after when I try to send something to that kind of websocket (with header probably wrong) I got an 400 error 'BAD REQUEST' does that mean that I succeded to connect but I send the wrong request ?

    – CYRUS
    Dec 31 '18 at 19:55











  • @CYRUS Yes, that's indeed what it means. You're connecting successfully, but after you're sending a HTTP request to 35.198.217.124, but you're using the wrong domain name www.example.com. That's why it fails.

    – EvE
    Dec 31 '18 at 21:44











  • Thanks for your answer that's obviously true but after multiple try with other host name I still get the bad request response do you have an idea of where the mistake could come from ?

    – CYRUS
    Jan 1 at 0:17











  • @CYRUS If you change to socket_connect() line to: echo socket_connect($socket, "93.184.216.34", 80) ? "true" : "false"; it DOES work when using 'Host: www.example.com'. 93.184.216.34 is the IP-address of www.example.com. If your connecting on port 443 you're using HTTPS and you need additional steps to setup a SSL connection. Unfortunately I don't now to setup HTTPS/SSL manually.

    – EvE
    Jan 1 at 2:58











  • @CYRUS Maybe the following link will help you: devdungeon.com/content/how-use-ssl-sockets-php

    – EvE
    Jan 1 at 3:02



















0














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 "truen";

fwrite($fp, "GET /api/v1/dp?symbol=ETH_BTC HTTP/1.1rn" .
"Host: engine.coss.iorn" .
"Accept: */*rn" .
"Connection: Upgradern" .
"Upgrade: websocketrnrn");
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"], .....





share|improve this answer
























  • Hi @EvE thanks you for your answer, you're right this works perfectly, but the thing is that is this case the request is a regular http request and I would never get informed of the changes on the website until I make a http request. Furthermore there is a system of units in the website http request cost 1 units where as websocket cost 0 units this is why I wanted to use them, I edited the post I think we are getting closer to the working code :)

    – CYRUS
    Jan 2 at 11:39













  • @CYRUS I currently have lack of time to look into it further, but just out of curiosity: How can it be that this site charges 1 units? Because I'm not even and a member of this site and I can also use https:// requests.

    – EvE
    Jan 4 at 12:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53989596%2funable-to-connect-to-websocket-using-wss-in-php%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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!






share|improve this answer
























  • Thanks a lot for your answer ! I will look at your link right after writing my answer. But I was wondering : I got the ip adress of the web socket then I made a line in php : echo socket_connect($socket ,"35.198.217.124",443)?"true":"non" and i got true but after when I try to send something to that kind of websocket (with header probably wrong) I got an 400 error 'BAD REQUEST' does that mean that I succeded to connect but I send the wrong request ?

    – CYRUS
    Dec 31 '18 at 19:55











  • @CYRUS Yes, that's indeed what it means. You're connecting successfully, but after you're sending a HTTP request to 35.198.217.124, but you're using the wrong domain name www.example.com. That's why it fails.

    – EvE
    Dec 31 '18 at 21:44











  • Thanks for your answer that's obviously true but after multiple try with other host name I still get the bad request response do you have an idea of where the mistake could come from ?

    – CYRUS
    Jan 1 at 0:17











  • @CYRUS If you change to socket_connect() line to: echo socket_connect($socket, "93.184.216.34", 80) ? "true" : "false"; it DOES work when using 'Host: www.example.com'. 93.184.216.34 is the IP-address of www.example.com. If your connecting on port 443 you're using HTTPS and you need additional steps to setup a SSL connection. Unfortunately I don't now to setup HTTPS/SSL manually.

    – EvE
    Jan 1 at 2:58











  • @CYRUS Maybe the following link will help you: devdungeon.com/content/how-use-ssl-sockets-php

    – EvE
    Jan 1 at 3:02
















0














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!






share|improve this answer
























  • Thanks a lot for your answer ! I will look at your link right after writing my answer. But I was wondering : I got the ip adress of the web socket then I made a line in php : echo socket_connect($socket ,"35.198.217.124",443)?"true":"non" and i got true but after when I try to send something to that kind of websocket (with header probably wrong) I got an 400 error 'BAD REQUEST' does that mean that I succeded to connect but I send the wrong request ?

    – CYRUS
    Dec 31 '18 at 19:55











  • @CYRUS Yes, that's indeed what it means. You're connecting successfully, but after you're sending a HTTP request to 35.198.217.124, but you're using the wrong domain name www.example.com. That's why it fails.

    – EvE
    Dec 31 '18 at 21:44











  • Thanks for your answer that's obviously true but after multiple try with other host name I still get the bad request response do you have an idea of where the mistake could come from ?

    – CYRUS
    Jan 1 at 0:17











  • @CYRUS If you change to socket_connect() line to: echo socket_connect($socket, "93.184.216.34", 80) ? "true" : "false"; it DOES work when using 'Host: www.example.com'. 93.184.216.34 is the IP-address of www.example.com. If your connecting on port 443 you're using HTTPS and you need additional steps to setup a SSL connection. Unfortunately I don't now to setup HTTPS/SSL manually.

    – EvE
    Jan 1 at 2:58











  • @CYRUS Maybe the following link will help you: devdungeon.com/content/how-use-ssl-sockets-php

    – EvE
    Jan 1 at 3:02














0












0








0







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!






share|improve this answer













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!







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 31 '18 at 19:33









EvEEvE

610111




610111













  • Thanks a lot for your answer ! I will look at your link right after writing my answer. But I was wondering : I got the ip adress of the web socket then I made a line in php : echo socket_connect($socket ,"35.198.217.124",443)?"true":"non" and i got true but after when I try to send something to that kind of websocket (with header probably wrong) I got an 400 error 'BAD REQUEST' does that mean that I succeded to connect but I send the wrong request ?

    – CYRUS
    Dec 31 '18 at 19:55











  • @CYRUS Yes, that's indeed what it means. You're connecting successfully, but after you're sending a HTTP request to 35.198.217.124, but you're using the wrong domain name www.example.com. That's why it fails.

    – EvE
    Dec 31 '18 at 21:44











  • Thanks for your answer that's obviously true but after multiple try with other host name I still get the bad request response do you have an idea of where the mistake could come from ?

    – CYRUS
    Jan 1 at 0:17











  • @CYRUS If you change to socket_connect() line to: echo socket_connect($socket, "93.184.216.34", 80) ? "true" : "false"; it DOES work when using 'Host: www.example.com'. 93.184.216.34 is the IP-address of www.example.com. If your connecting on port 443 you're using HTTPS and you need additional steps to setup a SSL connection. Unfortunately I don't now to setup HTTPS/SSL manually.

    – EvE
    Jan 1 at 2:58











  • @CYRUS Maybe the following link will help you: devdungeon.com/content/how-use-ssl-sockets-php

    – EvE
    Jan 1 at 3:02



















  • Thanks a lot for your answer ! I will look at your link right after writing my answer. But I was wondering : I got the ip adress of the web socket then I made a line in php : echo socket_connect($socket ,"35.198.217.124",443)?"true":"non" and i got true but after when I try to send something to that kind of websocket (with header probably wrong) I got an 400 error 'BAD REQUEST' does that mean that I succeded to connect but I send the wrong request ?

    – CYRUS
    Dec 31 '18 at 19:55











  • @CYRUS Yes, that's indeed what it means. You're connecting successfully, but after you're sending a HTTP request to 35.198.217.124, but you're using the wrong domain name www.example.com. That's why it fails.

    – EvE
    Dec 31 '18 at 21:44











  • Thanks for your answer that's obviously true but after multiple try with other host name I still get the bad request response do you have an idea of where the mistake could come from ?

    – CYRUS
    Jan 1 at 0:17











  • @CYRUS If you change to socket_connect() line to: echo socket_connect($socket, "93.184.216.34", 80) ? "true" : "false"; it DOES work when using 'Host: www.example.com'. 93.184.216.34 is the IP-address of www.example.com. If your connecting on port 443 you're using HTTPS and you need additional steps to setup a SSL connection. Unfortunately I don't now to setup HTTPS/SSL manually.

    – EvE
    Jan 1 at 2:58











  • @CYRUS Maybe the following link will help you: devdungeon.com/content/how-use-ssl-sockets-php

    – EvE
    Jan 1 at 3:02

















Thanks a lot for your answer ! I will look at your link right after writing my answer. But I was wondering : I got the ip adress of the web socket then I made a line in php : echo socket_connect($socket ,"35.198.217.124",443)?"true":"non" and i got true but after when I try to send something to that kind of websocket (with header probably wrong) I got an 400 error 'BAD REQUEST' does that mean that I succeded to connect but I send the wrong request ?

– CYRUS
Dec 31 '18 at 19:55





Thanks a lot for your answer ! I will look at your link right after writing my answer. But I was wondering : I got the ip adress of the web socket then I made a line in php : echo socket_connect($socket ,"35.198.217.124",443)?"true":"non" and i got true but after when I try to send something to that kind of websocket (with header probably wrong) I got an 400 error 'BAD REQUEST' does that mean that I succeded to connect but I send the wrong request ?

– CYRUS
Dec 31 '18 at 19:55













@CYRUS Yes, that's indeed what it means. You're connecting successfully, but after you're sending a HTTP request to 35.198.217.124, but you're using the wrong domain name www.example.com. That's why it fails.

– EvE
Dec 31 '18 at 21:44





@CYRUS Yes, that's indeed what it means. You're connecting successfully, but after you're sending a HTTP request to 35.198.217.124, but you're using the wrong domain name www.example.com. That's why it fails.

– EvE
Dec 31 '18 at 21:44













Thanks for your answer that's obviously true but after multiple try with other host name I still get the bad request response do you have an idea of where the mistake could come from ?

– CYRUS
Jan 1 at 0:17





Thanks for your answer that's obviously true but after multiple try with other host name I still get the bad request response do you have an idea of where the mistake could come from ?

– CYRUS
Jan 1 at 0:17













@CYRUS If you change to socket_connect() line to: echo socket_connect($socket, "93.184.216.34", 80) ? "true" : "false"; it DOES work when using 'Host: www.example.com'. 93.184.216.34 is the IP-address of www.example.com. If your connecting on port 443 you're using HTTPS and you need additional steps to setup a SSL connection. Unfortunately I don't now to setup HTTPS/SSL manually.

– EvE
Jan 1 at 2:58





@CYRUS If you change to socket_connect() line to: echo socket_connect($socket, "93.184.216.34", 80) ? "true" : "false"; it DOES work when using 'Host: www.example.com'. 93.184.216.34 is the IP-address of www.example.com. If your connecting on port 443 you're using HTTPS and you need additional steps to setup a SSL connection. Unfortunately I don't now to setup HTTPS/SSL manually.

– EvE
Jan 1 at 2:58













@CYRUS Maybe the following link will help you: devdungeon.com/content/how-use-ssl-sockets-php

– EvE
Jan 1 at 3:02





@CYRUS Maybe the following link will help you: devdungeon.com/content/how-use-ssl-sockets-php

– EvE
Jan 1 at 3:02













0














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 "truen";

fwrite($fp, "GET /api/v1/dp?symbol=ETH_BTC HTTP/1.1rn" .
"Host: engine.coss.iorn" .
"Accept: */*rn" .
"Connection: Upgradern" .
"Upgrade: websocketrnrn");
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"], .....





share|improve this answer
























  • Hi @EvE thanks you for your answer, you're right this works perfectly, but the thing is that is this case the request is a regular http request and I would never get informed of the changes on the website until I make a http request. Furthermore there is a system of units in the website http request cost 1 units where as websocket cost 0 units this is why I wanted to use them, I edited the post I think we are getting closer to the working code :)

    – CYRUS
    Jan 2 at 11:39













  • @CYRUS I currently have lack of time to look into it further, but just out of curiosity: How can it be that this site charges 1 units? Because I'm not even and a member of this site and I can also use https:// requests.

    – EvE
    Jan 4 at 12:32
















0














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 "truen";

fwrite($fp, "GET /api/v1/dp?symbol=ETH_BTC HTTP/1.1rn" .
"Host: engine.coss.iorn" .
"Accept: */*rn" .
"Connection: Upgradern" .
"Upgrade: websocketrnrn");
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"], .....





share|improve this answer
























  • Hi @EvE thanks you for your answer, you're right this works perfectly, but the thing is that is this case the request is a regular http request and I would never get informed of the changes on the website until I make a http request. Furthermore there is a system of units in the website http request cost 1 units where as websocket cost 0 units this is why I wanted to use them, I edited the post I think we are getting closer to the working code :)

    – CYRUS
    Jan 2 at 11:39













  • @CYRUS I currently have lack of time to look into it further, but just out of curiosity: How can it be that this site charges 1 units? Because I'm not even and a member of this site and I can also use https:// requests.

    – EvE
    Jan 4 at 12:32














0












0








0







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 "truen";

fwrite($fp, "GET /api/v1/dp?symbol=ETH_BTC HTTP/1.1rn" .
"Host: engine.coss.iorn" .
"Accept: */*rn" .
"Connection: Upgradern" .
"Upgrade: websocketrnrn");
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"], .....





share|improve this answer













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 "truen";

fwrite($fp, "GET /api/v1/dp?symbol=ETH_BTC HTTP/1.1rn" .
"Host: engine.coss.iorn" .
"Accept: */*rn" .
"Connection: Upgradern" .
"Upgrade: websocketrnrn");
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"], .....






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 7:54









EvEEvE

610111




610111













  • Hi @EvE thanks you for your answer, you're right this works perfectly, but the thing is that is this case the request is a regular http request and I would never get informed of the changes on the website until I make a http request. Furthermore there is a system of units in the website http request cost 1 units where as websocket cost 0 units this is why I wanted to use them, I edited the post I think we are getting closer to the working code :)

    – CYRUS
    Jan 2 at 11:39













  • @CYRUS I currently have lack of time to look into it further, but just out of curiosity: How can it be that this site charges 1 units? Because I'm not even and a member of this site and I can also use https:// requests.

    – EvE
    Jan 4 at 12:32



















  • Hi @EvE thanks you for your answer, you're right this works perfectly, but the thing is that is this case the request is a regular http request and I would never get informed of the changes on the website until I make a http request. Furthermore there is a system of units in the website http request cost 1 units where as websocket cost 0 units this is why I wanted to use them, I edited the post I think we are getting closer to the working code :)

    – CYRUS
    Jan 2 at 11:39













  • @CYRUS I currently have lack of time to look into it further, but just out of curiosity: How can it be that this site charges 1 units? Because I'm not even and a member of this site and I can also use https:// requests.

    – EvE
    Jan 4 at 12:32

















Hi @EvE thanks you for your answer, you're right this works perfectly, but the thing is that is this case the request is a regular http request and I would never get informed of the changes on the website until I make a http request. Furthermore there is a system of units in the website http request cost 1 units where as websocket cost 0 units this is why I wanted to use them, I edited the post I think we are getting closer to the working code :)

– CYRUS
Jan 2 at 11:39







Hi @EvE thanks you for your answer, you're right this works perfectly, but the thing is that is this case the request is a regular http request and I would never get informed of the changes on the website until I make a http request. Furthermore there is a system of units in the website http request cost 1 units where as websocket cost 0 units this is why I wanted to use them, I edited the post I think we are getting closer to the working code :)

– CYRUS
Jan 2 at 11:39















@CYRUS I currently have lack of time to look into it further, but just out of curiosity: How can it be that this site charges 1 units? Because I'm not even and a member of this site and I can also use https:// requests.

– EvE
Jan 4 at 12:32





@CYRUS I currently have lack of time to look into it further, but just out of curiosity: How can it be that this site charges 1 units? Because I'm not even and a member of this site and I can also use https:// requests.

– EvE
Jan 4 at 12:32


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53989596%2funable-to-connect-to-websocket-using-wss-in-php%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas