For a deeper look into our Elektron API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
1 0 1 1

PHP - communication to Refinitiv via WebSocket protocol

Hello dear community,

I'm facing an issue... I am using Refinitive platform, where I am 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.

I am using php library TextTalk ofr websockets communication.

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();

?>
#technologywebsocketsrefinitiv-realtime-optimisedphp
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hello @lukaskosejk

Thank you for your participation in the forum. Is the reply below satisfactory in resolving your query?


If so please can you click the 'Accept' text next to the appropriate reply? This will guide all community members who have a similar question.

Thanks,


AHS

Please be informed that a reply has been verified as correct in answering the question, and has been marked as such.


Thanks,

AHS

1 Answer

· Write an Answer
Upvotes
Accepted
79.2k 251 52 74

@lukaskosejk

Thank you for reaching out to us.

The WebSocket code should be like this:

$socket = new WebSocket\Client('wss://eu-central-1-aws-1-sm.optimized-pricing-api.refinitiv.net/WebSocket:443',[  
    'headers' => [ // Additional headers, used to specify subprotocol
        'Sec-WebSocket-Protocol' => 'tr_json2']
    ]);
$socket->send('
{
  "Domain": "Login",
  "ID": 1,
  "Key": {
    "Elements": {
      "ApplicationId": "256",
      "AuthenticationToken": "' . $accessToken . '",
      "Position": "123.123.123.123"
    },
    "NameType": "AuthnToken"
  }
}');
echo $socket->receive();
$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";
$response = $socket->receive();


echo "Přijata zpráva: $response\n";
$response = $socket->receive();


echo "Přijata zpráva: $response\n";
$socket->close();


icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.