Authorization Required with PHP

I am new with binary and WebSocket technology. I have the below code but the problem is I get Authorization Required. I understood what the problem is, but not show I know how to solve the problem.

How can I authorize and then called Balance for example?

require DIR . '/vendor/autoload.php';
$loop = \React\EventLoop\Factory::create();
$connector = new \Ratchet\Client\Connector($loop);

$connector('wss://')->then(function ($conn) {
$conn->on('message', function ($msg) {
echo "ticks update: {$msg}\n";

$conn->send("{\"balance\": \"1\",\"subscribe\": \"1\"}");




  • You need to first authorize, and in $conn->on('message', check for a successful response then send the balance request.

        require __DIR__ . '/vendor/autoload.php';
        \Ratchet\Client\connect('wss://')->then(function($conn) {
            $conn->on('message', function($msg) use ($conn) {
               $result = json_decode($msg);
               if (!isset($result->{'error'}) && isset($result->{'authorize'})) {
                   $conn->send('{"balance" : 1 }');
                } else {
                echo "Result: {$msg}\n";
            $conn->send('{"authorize" : ""}');
        }, function ($e) {
            echo "Could not connect: {$e->getMessage()}\n";
  • thank you so much @michael_mueller , really safe me time.

    One last thing, do you know why each call is loading for almost 2minutes

    Many thanks in advance

  • Solved the loading problem by closing the connection after getting the result. @michael_mueller thanks once again

Sign In or Register to comment.