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
- 6 AHS
- 37 Alpha
- 161 App Studio
- 4 Block Chain
- 4 Bot Platform
- 16 Connected Risk APIs
- 47 Data Fusion
- 30 Data Model Discovery
- 608 Datastream
- 1.3K DSS
- 577 Eikon COM
- 4.9K Eikon Data APIs
- 7 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- Trading API
- 2.7K Elektron
- 1.3K EMA
- 236 ETA
- 519 WebSocket API
- 33 FX Venues
- 10 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 20 Messenger Bot
- 2 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 59 Open Calais
- 264 Open PermID
- 39 Entity Search
- 2 Org ID
- PAM
- PAM - Logging
- 8.4K Private Comments
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 20 RDMS
- 1.4K Refinitiv Data Platform
- 367 Refinitiv Data Platform Libraries
- 3 Refinitiv Due Diligence
- LSEG Due Diligence Portal API
- 3 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.1K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 10 World-Check Customer Risk Screener
- 990 World-Check One
- 44 World-Check One Zero Footprint
- 45 Side by Side Integration API
- Test Space
- 3 Thomson One Smart
- 1.2K TR Internal
- Global Hackathon 2015
- 2 Specialists Who Code
- 10 TR Knowledge Graph
- 150 Transactions
- 142 REDI API
- 1.7K TREP APIs
- 4 CAT
- 21 DACS Station
- 117 Open DACS
- 1.1K RFA
- 103 UPA
- 172 TREP Infrastructure
- 224 TRKD
- 886 TRTH
- 5 Velocity Analytics
- 5 Wealth Management Web Services
- 59 Workspace SDK
- 9 Element Framework
- 5 Grid
- 13 World-Check Data File
- Yield Book Analytics
- 46 中文论坛