How to get LTP: Sample Source Code in PHP, added a GetLTP() function [Error Handling Excluded]


  • Category PHP Language : How to get Last Trading Price in PHP , I changed the core smart_api.php class and added a PHP function in it. Part of class. This should be incuded in this class.

    I have changed SMART_API.php Class File and added a function as below

    Add inside Smart_API.php file in class

    public static function GetLTP($paramArray)
    {
    extract($paramArray);

    	//getToken() check whether userlogged in or not and return jwtToken
    	$token = self::getToken();
    			
    	 if ($token['status']) 
    	 {
    	 	//jwtToken not empty 
    		$jwtToken = $token['jwtToken'];
    
    		//get url from config file
    		// $UrlData = AngelConfigrationManage::AngelConfigrationData();
    		// $url = $UrlData['root'].$UrlData['candle_data'];	  	
    		
    		$url = "https://apiconnect.angelbroking.com/order-service/rest/secure/angelbroking/order/v1/getLtpData";
    		
    				
    		// Common function to call smart api
    		$response_data	=	self::CurlOperation($url,$paramArray, $jwtToken,'POST');
    	}
    	else{
    		$response_data['status'] = 'fail';
    		$response_data['error'] = 'The token is invalid';
    		$response_data	=	json_encode($response_data);
    	}
    	
    	return $response_data;
    }
    

    Then created a new PHP File test.php: Add the following code in it.

    Your PHP File :Test.php
    ** I am not using composer, so changed the codes little bit. commented autoload.php and changed smart_api.php namespace area

    require_once 'SmartApi.php';
    //require_once DIR . '/vendor/autoload.php';

    $smart_api = new \AngelBroking\SmartApi();
    $clientCode = "angel client code";
    $clientPass = "your angel password";

    $login = $smart_api ->GenerateSession($clientCode,$clientPass);

    echo "<p>Get LTP :</p>";
    $params= array("exchange"=>"NSE", "tradingsymbol"=>"SBIN-EQ","symboltoken"=>"3045");
    $ltp = $smart_api ->GetLTP($params);
    var_dump($ltp);

    //--------------- PHP Test File ennds --------------------
    The Response I have got for SBIN-EQ:

    Get LTP :

    string(256) "{"status":"success","http_code":200,"http_error":"","response_data":{"status":true,"message":"SUCCESS","errorcode":"","data":{"exchange":"NSE","tradingsymbol":"SBIN-EQ","symboltoken":"3045","open":443.7,"high":447,"low":436.7,"close":441.85,"ltp":437.95}}}"

    Its correct I compared with moneycotrol.com

    Developed by Bikram

    The Error Handling Not Included Here : I don't know when GetLTP() will fail, because of request limit to URL by a particuluar user or IP.

    The JSON Error String not mentioned here.. https://smartapi.angelbroking.com/docs/Instruments

    I was struggling to get sample codes here so developed myself, feel free to use it....Best Wishes


  • @admin

    I got LTP like this :

    string(256) "{"status":"success","http_code":200,"http_error":"","response_data":{"status":true,"message":"SUCCESS","errorcode":"","data":{"exchange":"NSE","tradingsymbol":"SBIN-EQ","symboltoken":"3045","open":443.7,"high":447,"low":436.7,"close":441.85,"ltp":437.95}}}"

    But what is
    OPEN HIGH LOW CLOSE in the response, is it daily OHLC or 1MIN or 1 HOURS or todays?