gopalindians/groww-php-sdk
Composer 安装命令:
composer require gopalindians/groww-php-sdk
包简介
PHP SDK for Groww Trading API
关键字:
README 文档
README
A PHP SDK for interacting with the Groww Trading API.
Installation
Install via Composer:
composer require gopalindians/groww-php-sdk
Usage
Authentication
use Groww\API\Client; // Initialize the client with your API key $groww = new Client('your_api_key_here'); // Optional: Enable logging $groww->setLogging(true, function($level, $message, $context) { // Custom logging implementation error_log("[$level] $message " . json_encode($context)); });
Trading
Place a new order
try { $orderData = [ 'validity' => 'DAY', 'exchange' => 'NSE', 'transaction_type' => 'BUY', 'order_type' => 'MARKET', 'price' => 0, 'product' => 'CNC', 'quantity' => 1, 'segment' => 'CASH', 'trading_symbol' => 'IDEA' ]; $result = $groww->orders()->create($orderData); print_r($result); } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error: " . $e->getMessage() . " (Code: " . $e->getErrorCode() . ")"; }
Get order details
try { $orderDetails = $groww->orders()->details('GMK39038RDT490CCVRO'); print_r($orderDetails); } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error: " . $e->getMessage() . " (Code: " . $e->getErrorCode() . ")"; }
Cancel an order
try { $result = $groww->orders()->cancel('GMK39038RDT490CCVRO'); print_r($result); } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error: " . $e->getMessage() . " (Code: " . $e->getErrorCode() . ")"; }
Portfolio Management
Get holdings
try { $holdings = $groww->portfolio()->holdings(); print_r($holdings); } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error: " . $e->getMessage() . " (Code: " . $e->getErrorCode() . ")"; }
Get positions
try { $positions = $groww->portfolio()->positions(); print_r($positions); } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error: " . $e->getMessage() . " (Code: " . $e->getErrorCode() . ")"; }
Market Data
Search for instruments
try { $searchResults = $groww->instruments()->search('RELIANCE'); print_r($searchResults); } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error: " . $e->getMessage() . " (Code: " . $e->getErrorCode() . ")"; }
Get live quotes
try { $quotes = $groww->liveData()->quotes(['RELIANCE', 'IDEA']); print_r($quotes); } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error: " . $e->getMessage() . " (Code: " . $e->getErrorCode() . ")"; }
Get historical data
try { $candleData = $groww->historicalData()->candles( 'RELIANCE', '1d', '2023-01-01', '2023-01-31' ); print_r($candleData); } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error: " . $e->getMessage() . " (Code: " . $e->getErrorCode() . ")"; }
Margin Information
try { $availableMargin = $groww->margin()->available(); print_r($availableMargin); } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error: " . $e->getMessage() . " (Code: " . $e->getErrorCode() . ")"; }
Error Handling
The SDK throws GrowwApiException when an error occurs. You can catch this exception to handle errors gracefully:
try { // API operations } catch (Groww\API\Exceptions\GrowwRateLimitException $e) { // Handle rate limiting specifically echo "Rate limit exceeded. Try again after " . $e->getWaitTime() . " seconds.\n"; sleep($e->getWaitTime()); // Retry the request } catch (Groww\API\Exceptions\GrowwApiException $e) { echo "Error message: " . $e->getMessage() . "\n"; echo "Error code: " . $e->getErrorCode() . "\n"; // Handle different error codes switch ($e->getErrorCode()) { case 'GA001': echo "Bad request - check your parameters\n"; break; case 'GA005': echo "Authentication error - check your API key\n"; break; default: echo "Unknown error occurred\n"; break; } }
Security Features
This SDK implements several security best practices:
- Input Validation: All inputs are validated before being sent to the API.
- TLS/SSL Verification: HTTPS connections are enforced by default.
- Rate Limiting Protection: Built-in rate limiting with exponential backoff.
- Parameter Sanitization: All parameters are sanitized to prevent injection attacks.
- Sensitive Data Protection: API keys and other sensitive data are redacted in logs.
- Error Handling: Comprehensive error handling for security-related issues.
Secure Logging
The SDK includes a secure logging system that redacts sensitive information:
// Enable logging with a custom logger $groww->setLogging(true, function($level, $message, $context) { // Custom logging implementation // All sensitive data is automatically redacted });
Testing
The SDK comes with a comprehensive test suite. To run the tests:
- Install development dependencies:
composer install --dev
- Run PHPUnit:
./vendor/bin/phpunit
Continuous Integration
This project uses GitHub Actions for continuous integration. Every time code is pushed to the main or master branch, or when a pull request is created against these branches, the test suite is automatically executed on multiple PHP versions (7.4, 8.0, and 8.1).
The CI pipeline:
- Sets up the PHP environment
- Installs dependencies via Composer
- Runs the PHPUnit test suite
You can check the workflow configuration in the .github/workflows/php-tests.yml file.
Writing Your Own Tests
You can use the existing test suite as a reference for writing your own tests. The SDK provides mock responses and helpers to make testing easier:
use Groww\API\Tests\TestCase; use Groww\API\Client; use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; use GuzzleHttp\Client as HttpClient; class YourTest extends TestCase { public function testYourMethod() { // Create a mock response $mockResponse = $this->createSuccessResponse(['data' => 'value']); // Set up mock handler $mock = new MockHandler([ new Response(200, [], json_encode($mockResponse)) ]); $handlerStack = HandlerStack::create($mock); $httpClient = new HttpClient(['handler' => $handlerStack]); // Create client with mock $client = new Client($this->getTestApiKey()); $client->setHttpClient($httpClient); // Test your code $result = $client->get('/endpoint'); // Assert results $this->assertEquals($mockResponse, $result); } }
API Documentation
For the full API reference, visit the Groww API documentation at: https://groww.in/trade-api/docs/curl
License
MIT
gopalindians/groww-php-sdk 适用场景与选型建议
gopalindians/groww-php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「sdk」 「finance」 「trading」 「stocks」 「groww」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 gopalindians/groww-php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gopalindians/groww-php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 gopalindians/groww-php-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Sila PHP SDK for API Version 0.2
Simple OFX file parser
PHP SDK for Ledga.io - Programmatic double-entry ledgers for finance, gaming and multi-tenant SaaS
A PSR-7 compatible library for making CRUD API endpoints
Fitbank SDK for PHP
PHP wrapper of the dekopay finance API
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-30