Please reply on query.
Please send instrument key for BSE sensex data (show real time sensex data)
i want to show only last trading price of sensex what to do.currently getting full array of value.
<?php namespace Com\Upstox\Marketdatafeeder\Rpc\Proto; //require_once(__DIR__ . '/../vendor/autoload.php'); require_once ('C:/xampp/htdocs/new_change/vendor/autoload.php'); use GuzzleHttp\Client; use WebSocket\Client as WebSocketClient; use Upstox\Client\Configuration; use Upstox\Client\Api\WebsocketApi; use Com\Upstox\Marketdatafeeder\Rpc\Proto\FeedResponse; require 'C:/xampp/htdocs/new_change/examples/websocket/market_data/protobuf/Com/Upstox/Marketdatafeeder/Rpc/Proto/FeedResponse.php'; /** * Function to decode Protobuf messages. * * @param string $buffer The protobuf message as a string. * @return FeedResponse The decoded FeedResponse object. */ function decodeProtobuf($buffer) { $feedResponse = new FeedResponse(); $feedResponse->mergeFromString($buffer); return $feedResponse; } /** * Function to get market data feed authorization. * * @param string $apiVersion The API version. * @param Configuration $configuration The configuration object. * @return array The API response. */ function getMarketDataFeedAuthorize($apiVersion, $configuration) { $apiInstance = new WebsocketApi( new Client(), $configuration ); $apiResponse = $apiInstance->getMarketDataFeedAuthorize($apiVersion); //echo ""; //echo json_encode($apiResponse); return $apiResponse; //echo ""; } /** * Function to fetch market updates. */ function fetchMarketUpdates() { $apiVersion = '2.0'; $accessToken = '*****************************'; // Configure with your access token $configuration = Configuration::getDefaultConfiguration(); $configuration->setAccessToken($accessToken); // Get the authorized URL for the WebSocket connection $response = getMarketDataFeedAuthorize($apiVersion, $configuration); // Create a WebSocket client instance with the authorized URL and disable SSL verification $client = new WebSocketClient($response['data']['authorized_redirect_uri'], [ 'timeout' => 10, 'context' => stream_context_create([ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false ] ]) ]); echo "Connection successful!\n"; $client->setTimeout(1); // Wait for 1 second // Message payload to send to the server $data = [ "guid" => "someguid", "method" => "sub", "data" => [ "mode" => "full", //"instrumentKeys" => ["NSE_INDEX|Nifty 50"] "instrumentKeys" => ["BSE_INDEX|SENSEX"] ] ]; // Send the data as binary $client->binary(json_encode($data)); // Continuously receive updates until an error occurs while (true) { try { // Receive and decode the incoming message $message = $client->receive(); $decodedData = decodeProtobuf($message); // Convert the decoded data to JSON and print it var_dump($decodedData->serializeToJsonString()); //var_dump($decodedData->serializeToJsonString()); } catch (\Exception $e) { // Print the error message and exit the loop if an error occurs echo "Error: {$e->getMessage()}\n"; break; } } } // Start fetching market updates fetchMarketUpdates();
To obtain LTP data exclusively, set the mode parameter to ltpc in your request.
{
"guid": "someguid",
"method": "sub",
"data": {
"mode": "ltpc",
"instrumentKeys": ["{your_instrument_key}"]
}
}
For a comprehensive understanding, it’s recommended to refer to the detailed guidance provided in the Upstox API documentation, specifically the section on Market Data Feed, available at this link: Upstox API Documentation.
getting this output
{“feeds”:{“BSE_INDEX|SENSEX”:{“ltpc”:{“ltp”:72257.740000000005,“ltt”:“1703836655524”,“cp”:72410.380000000005}}}}{“type”:“live_feed”,“feeds”:{“BSE_INDEX|SENSEX”:{“ltpc”:{“ltp”:72256.25,“ltt”:“1703836656526”,“cp”:72410.380000000005}}}}{“type”:“live_feed”,“feeds”:{“BSE_INDEX|SENSEX”:{“ltpc”:{“ltp”:72256.320000000007,“ltt”:“1703836657527”,“cp”:72410.380000000005}}}}{“type”:“live_feed”,“feeds”:{“BSE_INDEX|SENSEX”:{“ltpc”:{“ltp”:72249.899999999994,“ltt”:“1703836658529”,“cp”:72410.380000000005}}}}
i want only last trading price (ltp) only one value
why this error getting ?
Fatal error : Uncaught Upstox\Client\ApiException: [429] Client error: GET https://api.upstox.com/v2/feed/market-data-feed/authorize
resulted in a 429 Too Many Requests
response: {“status”:“error”,“errors”:[{“errorCode”:“UDAPI10005”,“message”:“Too Many Request Sent”,“propertyPath”:null,"invalidValu (truncated…) in C:\xampp\htdocs\new_change\lib\Api\WebsocketApi.php:392 Stack trace: #0 C:\xampp\htdocs\new_change\lib\Api\WebsocketApi.php(367): Upstox\Client\Api\WebsocketApi->getMarketDataFeedAuthorizeWithHttpInfo(‘2.0’) #1 C:\xampp\htdocs\new_change\examples\websocket\market_data\websocket_client.php(41): Upstox\Client\Api\WebsocketApi->getMarketDataFeedAuthorize(‘2.0’) #2 C:\xampp\htdocs\new_change\examples\websocket\market_data\websocket_client.php(61): Com\Upstox\Marketdatafeeder\Rpc\Proto\getMarketDataFeedAuthorize(‘2.0’, Object(Upstox\Client\Configuration)) #3 C:\xampp\htdocs\new_change\examples\websocket\market_data\websocket_client.php(136): Com\Upstox\Marketdatafeeder\Rpc\Proto\fetchMarketUpdates() #4 {main} thrown in C:\xampp\htdocs\new_change\lib\Api\WebsocketApi.php on line 392
Access token expiry can we increase or not.it is expired every day then what to do with this API for real time Sensex data.
please reply on query.
- You should only select the ‘ltp’ field from the response form below and disregard any other fields unless they are relevant to your specific use case. For detailed information about each field, refer to https://upstox.com/developer/api-documentation/get-market-data-feed#message-structure.
{
"type": "live_feed",
"feeds": {
"BSE_INDEX|SENSEX": {
"ltpc": {
"ltp": 72257.74,
"ltt": "1703836655524",
"cp": 72410.38
}
}
}
},
- The 429 status code indicates that there have been too many requests. Please review your code to ensure that your requests comply with the limits specified at https://upstox.com/developer/api-documentation/rate-limiting.
- The
access_token
acquired via this API remains valid until3:30 AM
the next day, irrespective of the time of its creation. A new access token must be generated through the authentication process after this period.
i am getting this error do you have any idea why tis is getting Fatal error : Uncaught Error: Class “ComposerAutoloaderInit5b76320925d18099d55a7ca1c8a399cd” not found in /home/u309871816/domains/asmlmgroup.in/public_html/vendor/autoload.php:26 Stack trace: #0 /home/u309871816/domains/asmlmgroup.in/public_html/vendor/upstox/upstox-php-sdk/examples/websocket/market_data/websocket_client.php(3): require_once() #1 {main} thrown in /home/u309871816/domains/asmlmgroup.in/public_html/vendor/autoload.php on line 26
websocket client code
<?php require_once('/home/u309871816/domains/asmlmgroup.in/public_html/vendor/autoload.php'); use GuzzleHttp\Client; use WebSocket\Client as WebSocketClient; use Upstox\Client\Configuration; use Upstox\Client\Api\WebsocketApi; use Com\Upstox\Marketdatafeeder\Rpc\Proto\FeedResponse; /** * Function to decode Protobuf messages. * * @param string $buffer The protobuf message as a string. * @return FeedResponse The decoded FeedResponse object. */ function decodeProtobuf($buffer) { $feedResponse = new FeedResponse(); $feedResponse->mergeFromString($buffer); return $feedResponse; } /** * Function to get market data feed authorization. * * @param string $apiVersion The API version. * @param Configuration $configuration The configuration object. * @return array The API response. */ function getMarketDataFeedAuthorize($apiVersion, $configuration) { $apiInstance = new WebsocketApi( new Client(), $configuration ); $apiResponse = $apiInstance->getMarketDataFeedAuthorize($apiVersion); return $apiResponse; } /** * Function to fetch market updates. */ function fetchMarketUpdates() { $apiVersion = '2.0'; $accessToken = 'eyJ0. etc.kfvk'; // Configure with your access token $configuration = Configuration::getDefaultConfiguration(); $configuration->setAccessToken($accessToken); // Get the authorized URL for the WebSocket connection $response = getMarketDataFeedAuthorize($apiVersion, $configuration); // Create a WebSocket client instance with the authorized URL and disable SSL verification $client = new WebSocketClient($response['data']['authorized_redirect_uri'], [ 'timeout' => 10, 'context' => stream_context_create([ 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false ] ]) ]); echo "Connection successful!\n"; $client->setTimeout(1); // Wait for 1 second // Message payload to send to the server $data = [ "guid" => "someguid", "method" => "sub", "data" => [ "mode" => "full", "instrumentKeys" => ["NSE_INDEX|Nifty Bank", "NSE_INDEX|Nifty 50"] ] ]; // Send the data as binary $client->binary(json_encode($data)); // Continuously receive updates until an error occurs while (true) { try { // Receive and decode the incoming message $message = $client->receive(); $decodedData = decodeProtobuf($message); // Convert the decoded data to JSON and print it var_dump($decodedData->serializeToJsonString()); } catch (\Exception $e) { // Print the error message and exit the loop if an error occurs echo "Error: {$e->getMessage()}\n"; break; } } } // Start fetching market updates fetchMarketUpdates();Auto load
<?php // autoload.php @generated by Composer if (PHP_VERSION_ID < 50600) { if (!headers_sent()) { header('HTTP/1.1 500 Internal Server Error'); } $err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL; if (!ini_get('display_errors')) { if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { fwrite(STDERR, $err); } elseif (!headers_sent()) { echo $err; } } trigger_error( $err, E_USER_ERROR ); } require_once(__DIR__ . '/../vendor/autoload.php'); return ComposerAutoloaderInit5b76320925d18099d55a7ca1c8a399cd::getLoader();@sandesh We’ve recently made updates to the PHP websocket code, which you can find here: upstox-php/examples/websocket/market_data at master · upstox/upstox-php · GitHub. Have you had a chance to try it out?
this API not getting real time sensex data
<?php $curl = curl_init(); // Set the URL curl_setopt($curl, CURLOPT_URL, 'https://api.upstox.com/v2/feed/market-data-feed'); // Set the request type to GET curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); // Set headers $headers = array( 'Content-Type: application/json', // Assuming JSON API 'Authorization: Bearer ***********************' // Replace with your actual access token ); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // Return the response instead of printing it curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Execute the request $response = curl_exec($curl); // Check for errors if (curl_errno($curl)) { echo 'Curl error: ' . curl_error($curl); } // Close cURL session curl_close($curl); // Output the response echo $response; ?>You need to integrate the WebSocket for market data for real-time API.
For an introduction to WebSockets, you can refer to the documentation at Market Data Feed | Upstox Developer API
Additionally, we provide sample websocket code to help you begin at Sample Implementation | Upstox Developer API
i want API to save access token automatically in file after it will expired
@isolution Could you clarify what you mean by automatically saving the access token to a file once it expires, and what specific scenario or application you aim to address with this approach?
access token expired daily at 3 Am then my sensex array shows zero.after generate new access token then it will display data.i want one API which to be call at 3 Am and generate access token and we save this access token in database so we show data after 3 AM also.
plz reply
Upstox API does not support or recommend the automation of access token generation in light of SEBI and regulatory guidelines. Users are required to manually login once daily by adhering to the specified login procedures.
Thank you.
I want to get gold,silver and crude oill real time data like this
Like below?
$specificData = [
‘BSE_INDEX|SENSEX’ => $dataArray[‘feeds’][‘BSE_INDEX|SENSEX’][‘ltpc’],
‘NSE_INDEX|Nifty 50’ => $dataArray[‘feeds’][‘NSE_INDEX|Nifty 50’][‘ltpc’],
‘NSE_INDEX|Nifty Midcap 50’ => $dataArray[‘feeds’][‘NSE_INDEX|Nifty Midcap 50’][‘ltpc’],
‘MCX_FO|428108’ => $dataArray[‘feeds’][‘MCX_FO|428108’][‘ltpc’],
];
To receive real-time data, you must utilize websockets. For integration, you can refer to the following documentation: Market Data Feed | Upstox Developer API.
For seamless integration, you may also consult the SDKs available at:
Python: GitHub - upstox/upstox-python: Official Python SDK for accessing Upstox API
Node.js: GitHub - upstox/upstox-nodejs: Official NodeJS library for accessing Upstox APIs
Java: GitHub - upstox/upstox-java: Official Java SDK for accessing Upstox API
PHP: GitHub - upstox/upstox-php: Official PHP SDK for accessing Upstox API