定制 nishantwebdev/nse-stock-data-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nishantwebdev/nse-stock-data-php

Composer 安装命令:

composer require nishantwebdev/nse-stock-data-php

包简介

A comprehensive PHP library for accessing NSE (National Stock Exchange) India stock market data

README 文档

README

A comprehensive PHP library for accessing NSE (National Stock Exchange) India stock market data. This library provides easy-to-use methods to fetch real-time and historical stock data, and more.

Features

  • Real-time Stock Data: Get current prices, volume, and market data
  • Historical Data: Fetch historical stock prices and trading data
  • Holiday Calendar: Check trading holidays and market status
  • Intraday Data: Access real-time intraday price movements

Installation

Using Composer (Recommended)

composer require nishantwebdev/nse-stock-data-php

Manual Installation

  1. Clone this repository:
git clone https://github.com/nishantwebdev/nse-stock-data-php.git
cd nse-stock-data-php
  1. Install dependencies:
composer install

Quick Start

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";
}

Usage Examples

Main Methods

getEquityDetails(string $symbol): EquityDetails

Get comprehensive equity information including price, volume, and company details.

getEquityHistoricalData(string $symbol, DateRange $range): array

Get historical stock data for a specified date range.

getEquityIntradayData(string $symbol, bool $isPreOpenData = false): IntradayData

Get real-time intraday price data.

getIndexOptionChain(string $indexSymbol): OptionChainData

Get option chain data for indices (NIFTY, BANKNIFTY, etc.).

getEquityOptionChain(string $symbol): OptionChainData

Get option chain data for individual stocks.

getDerivativeData(string $symbol): array

Get futures and options data for a stock.

checkHoliday(DateTime $date): bool

Check if a given date is a trading holiday.

getAllStockSymbols(): array

Get list of all available stock symbols.

getHolidayData(): array

Get holiday data for the current year (cached automatically).

getHolidayDataForYear(int $year): array

Get holiday data for a specific year.

clearHolidayCache(?int $year = null): bool

Clear holiday cache for a specific year or all years.

Complete Method Documentation

Holiday and Market Status Methods

checkHoliday(DateTime $date): bool

Check if a given date is a trading holiday. Returns true if the date is a holiday (weekend or trading holiday), false otherwise.

Parameters:

  • $date (DateTime): The date to check

Returns: bool - true if holiday, false if trading day

Example:

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

Get current market status including market state, trade date, and other market information.

Returns: MarketStatus - Object containing market status details

Example:

$marketStatus = $nse->getMarketStatus();

Index Methods

getAllIndices(): array<Index>

Get all available market indices with their current values and changes.

Returns: array - Array of Index objects containing index information

Example:

$indices = $nse->getAllIndices();
getEquityStockIndices(string $index): IndexDetails

Get detailed information for a specific equity stock index.

Parameters:

  • $index (string): The index symbol (e.g., 'NIFTY 50', 'BANK NIFTY')

Returns: IndexDetails - Object containing detailed index information

Example:

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

Equity Information Methods

getEquityDetails(string $symbol): EquityDetails

Get comprehensive equity information including price, volume, company details, and market data.

Parameters:

  • $symbol (string): Stock symbol (e.g., 'TCS', 'RELIANCE')

Returns: EquityDetails - Object containing complete equity information

Example:

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

Get detailed trade information for a specific equity including volume, value, and trade statistics.

Parameters:

  • $symbol (string): Stock symbol

Returns: EquityTradeInfo - Object containing trade information

Example:

$tradeInfo = $nse->getEquityTradeInfo('TCS');
getEquityCorporateInfo(string $symbol): EquityCorporateInfo

Get corporate information for a specific equity including company details, announcements, and corporate actions.

Parameters:

  • $symbol (string): Stock symbol

Returns: EquityCorporateInfo - Object containing corporate information

Example:

$corporateInfo = $nse->getEquityCorporateInfo('TCS');
getEquityIntradayData(string $symbol, bool $isPreOpenData = false): IntradayData

Get real-time intraday price data for a specific equity.

Parameters:

  • $symbol (string): Stock symbol
  • $isPreOpenData (bool, optional): Whether to fetch pre-open market data (default: false)

Returns: IntradayData - Object containing intraday price data

Example:

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

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

Historical Data Methods

getEquityHistoricalData(string $symbol, DateRange $range = null): array<EquityHistoricalData>

Get historical stock data for a specified date range. If no range is provided, defaults to the last month.

Parameters:

  • $symbol (string): Stock symbol
  • $range (DateRange, optional): Date range for historical data

Returns: array - Array of historical data objects

Example:

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;
    }
}
getEquityPriceByDate(string $symbol, DateTime $date): int|float

Get the closing price of a stock for a specific date. If the date is a holiday, returns the price from the last trading day.

Parameters:

  • $symbol (string): Stock symbol
  • $date (DateTime): The date to get price for

Returns: int|float - The closing price for the specified date

Throws: Exception if no data is found for the given date

Example:

$priceDate = new DateTime('2024-01-15');
$price = $nse->getEquityPriceByDate('TCS', $priceDate);
echo "TCS price on " . $priceDate->format('Y-m-d') . ": ₹" . $price;
getIndexHistoricalData(string $index, DateRange $range): array<IndexHistoricalData>

Get historical data for a specific index over a date range.

Parameters:

  • $index (string): Index symbol (e.g., 'NIFTY 50', 'BANK NIFTY')
  • $range (DateRange): Date range for historical data

Returns: array - Array of historical index data

Example:

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);

Holiday Data Management Methods

getHolidayData(): array

Get holiday data for the current year. Data is automatically cached to improve performance.

Returns: array - Array of holiday dates in 'd-M-Y' format

Example:

$holidays = $nse->getHolidayData();
foreach ($holidays as $holiday) {
    echo "Holiday: " . $holiday;
}
clearHolidayCache(?int $year = null): bool

Clear cached holiday data for a specific year or all years.

Parameters:

  • $year (int|null, optional): Year to clear cache for. If null, clears all cached data.

Returns: bool - true if cache was cleared successfully

Example:

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

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

Data Models

The library provides strongly-typed data models for all API responses:

  • EquityDetails: Complete equity information
  • EquityHistoricalData: Historical price data
  • OptionChainData: Option chain information
  • IntradayData: Real-time price data
  • DateRange: Date range specification

Error Handling

The library throws exceptions for various error conditions:

try {
    $equityDetails = $nse->getEquityDetails('INVALID_SYMBOL');
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}

Rate Limiting

The library includes built-in rate limiting and connection management to respect NSE's API limits. It automatically handles:

  • Connection pooling
  • Request throttling
  • Cookie management
  • Retry logic

Requirements

  • PHP 8.0 or higher

Examples

Check the examples/ directory for more detailed usage examples:

  • basic_usage.php: Basic stock data retrieval
  • historical_data.php: Historical data analysis
  • option_chain_analysis.php: Option chain analysis

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Disclaimer

This library is for educational and research purposes. Please ensure compliance with NSE's terms of service and applicable regulations when using this library for commercial purposes.

Support

For issues and questions:

  • Create an issue on GitHub
  • Check the examples directory

Changelog

See CHANGELOG.md for a list of changes and version history.

nishantwebdev/nse-stock-data-php 适用场景与选型建议

nishantwebdev/nse-stock-data-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「stock」 「finance」 「market」 「trading」 「india」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nishantwebdev/nse-stock-data-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nishantwebdev/nse-stock-data-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 nishantwebdev/nse-stock-data-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-23