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(); ?>
Best Answer
-
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();0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 685 Datastream
- 1.4K DSS
- 615 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 252 ETA
- 556 WebSocket API
- 38 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 652 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 193 TREP Infrastructure
- 228 TRKD
- 917 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛