Communication to Refinitiv via WebSocket protocol

74 Views Asked by At

I'm facing an issue... I am using Refinitive platform, where we're trying to fetch data from a WebSocket. First, we generate an access token through PHP, which is working fine. Then, we save it and include it in the JSON content. When calling the WSS endpoint, I'm encountering the following errors. Could someone please assist me? I'm using the textalk library for PHP WebSocket communication. I have also tried a calling by the postman and everything was great and working.

Please, can you help me?

Here is my used PHP code: (I replaced a secrets before asking a question)

Errors while running the script:

Uncaught WebSocket\ConnectionException: Connection to 'wss://eu-central-1-aws-1-sm.optimized-pricing-api.refinitiv.net' failed: Server sent invalid upgrade response: HTTP/1.1 400 Bad Request Content-Type: text/html; charset=UTF-8 Connection: close in C:\xampp\htdocs\api\vendor\textalk\websocket\lib\Client.php:441 Stack trace: #0 C:\xampp\htdocs\api\vendor\textalk\websocket\lib\Client.php(169): WebSocket\Client->connect() #1 C:\xampp\htdocs\api\get-data.php(51): WebSocket\Client->send('\r\n{\r\n "Domain"...') #2 {main} thrown in C:\xampp\htdocs\api\vendor\textalk\websocket\lib\Client.php on line 441

<?php

// Import knihoven
require 'vendor/autoload.php';

use WebSocket\Client;

// Definujeme proměnné
$clientId = 'myid';
$username = 'username';
$password = 'password_here';
$scope = 'trapi';

// Sestavíme request pro získání Access Tokenu
$requestBody = http_build_query([
    'grant_type' => 'password',
    'client_id' => $clientId,
    'username' => $username,
    'password' => $password,
    'scope' => $scope,
    'takeExclusiveSignOnControl' => 'true',
]);

$ch = curl_init('https://api.refinitiv.com/auth/oauth2/v1/token');
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/x-www-form-urlencoded',
]);

$response = curl_exec($ch);

if ($response === false) {
    die('Chyba při získávání Access Tokenu: ' . curl_error($ch));
}

$accessToken = json_decode($response, true)['access_token'];

echo "Access Token: $accessToken\n";`

// Vytvoříme WebSocket klienta
$socket = new WebSocket\Client('wss://eu-central-1-aws-1-sm.optimized-pricing-api.refinitiv.net:443');

$socket->send('
{
  "Domain": "Login",
  "ID": 1,
  "Key": {
    "Elements": {
      "ApplicationId": "056",
      "AuthenticationToken": "' . $accessToken . '",
      "Position": "123.123.123.123
    },
    "NameType": "AuthnToken"
  },
  "Host": "<calculated at runtime>",
  "Connection": "Upgrade",
  "Upgrade": "websocket",
  "Sec-WebSocket-Key": "<calculated at runtime>",
  "Sec-WebSocket-Version": "13",
  "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
  "User-Agent": "PHP",  // 
  "Sec-WebSocket-Protocol": "tr_json2",
  "Content-Type": "application/json" 
}
');

$socket->send('
{
  "ID": 2,
  "Key": {
    "Name": ["EURIRS", "CZKIRS"],
    "Service": "ELEKTRON_DD"
  },
  "Streaming": false
}
');

// Čekáme na odpověď
$response = $socket->receive();

echo "Přijata zpráva: $response\n";

$socket->close();

?>

`

0

There are 0 best solutions below