PHP code example of nishantwebdev / nse-stock-data-php

1. Go to this page and download the library: Download nishantwebdev/nse-stock-data-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nishantwebdev / nse-stock-data-php example snippets


use NseData\StockClient;
use NseData\DateRange;

// Initialize the NSE client
$nse = new StockClient();

try {
    // Get equity details for a stock
    $equityDetails = $nse->getEquityDetails('TCS');
    echo "Company: " . $equityDetails->info->companyName . "\n";
    echo "Last Price: ₹" . $equityDetails->priceInfo->lastPrice . "\n";
    echo "Change: " . $equityDetails->priceInfo->change . " (" . $equityDetails->priceInfo->pChange . "%)\n";
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

$testDate = new DateTime('2024-01-26'); // Republic Day
$isHoliday = $nse->checkHoliday($testDate);
echo "Is " . $testDate->format('Y-m-d') . " a holiday? " . ($isHoliday ? 'Yes' : 'No');

$marketStatus = $nse->getMarketStatus();

$indices = $nse->getAllIndices();

$niftyDetails = $nse->getEquityStockIndices('NIFTY 50');

$equityDetails = $nse->getEquityDetails('TCS');
echo "Company: " . $equityDetails->info->companyName;
echo "Last Price: ₹" . $equityDetails->priceInfo->lastPrice;
echo "Change: " . $equityDetails->priceInfo->change;

$tradeInfo = $nse->getEquityTradeInfo('TCS');

$corporateInfo = $nse->getEquityCorporateInfo('TCS');


// Regular intraday data
$intradayData = $nse->getEquityIntradayData('TCS');

// Pre-open market data
$preOpenData = $nse->getEquityIntradayData('TCS', true);

use NseData\DateRange;

$startDate = new DateTime('2024-01-01');
$endDate = new DateTime('2024-01-31');
$dateRange = new DateRange(['start' => $startDate, 'end' => $endDate]);

$historicalData = $nse->getEquityHistoricalData('TCS', $dateRange);

foreach ($historicalData as $data) {
    foreach ($data->data as $record) {
        echo "Date: " . $record->CH_TIMESTAMP;
        echo "Open: ₹" . $record->CH_OPENING_PRICE;
        echo "High: ₹" . $record->CH_TRADE_HIGH_PRICE;
        echo "Low: ₹" . $record->CH_TRADE_LOW_PRICE;
        echo "Close: ₹" . $record->CH_CLOSING_PRICE;
        echo "Volume: " . $record->CH_TOT_TRADED_QTY;
    }
}

$priceDate = new DateTime('2024-01-15');
$price = $nse->getEquityPriceByDate('TCS', $priceDate);
echo "TCS price on " . $priceDate->format('Y-m-d') . ": ₹" . $price;

use NseData\DateRange;

$startDate = new DateTime('2024-01-01');
$endDate = new DateTime('2024-01-31');
$dateRange = new DateRange(['start' => $startDate, 'end' => $endDate]);

$indexData = $nse->getIndexHistoricalData('NIFTY 50', $dateRange);


$holidays = $nse->getHolidayData();
foreach ($holidays as $holiday) {
    echo "Holiday: " . $holiday;
}

// Clear cache for specific year
$nse->clearHolidayCache(2024);

// Clear all cached holiday data
$nse->clearHolidayCache();

try {
    $equityDetails = $nse->getEquityDetails('INVALID_SYMBOL');
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}
bash
git clone https://github.com/nishantwebdev/nse-stock-data-php.git
cd nse-stock-data-php