Skip to content

Commit 8a46118

Browse files
authored
Merge branch 'release-5.0.0' into master
2 parents ec01516 + 4b5878b commit 8a46118

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+3479
-2052
lines changed

.github/workflows/php-test.yml

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,35 @@ jobs:
55
test:
66
strategy:
77
matrix:
8-
php-versions: ["8.0", "8.1"]
8+
php-versions: ["8.0", "8.1", "8.2", "8.3"]
99
os: ["ubuntu-latest", "macos-latest"]
10-
phpunit-versions: ["9.5.4"]
1110

1211
runs-on: ${{ matrix.os }}
1312

1413
steps:
15-
- uses: actions/checkout@v2
14+
- uses: actions/checkout@v4
1615

1716
- name: Setup PHP
1817
uses: shivammathur/setup-php@v2
1918
with:
2019
php-version: ${{ matrix.php-versions }}
20+
extensions: curl, json, zlib
21+
coverage: none
2122

22-
- name: Install composer dependencies
23-
run: composer install --no-scripts
23+
- name: Validate composer.json and composer.lock
24+
run: composer validate --strict
25+
26+
- name: Cache Composer packages
27+
id: composer-cache
28+
uses: actions/cache@v3
29+
with:
30+
path: vendor
31+
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.lock') }}
32+
restore-keys: |
33+
${{ runner.os }}-php-${{ matrix.php-versions }}-
2434
25-
- name: Install PHPUnit
26-
run: composer global require "phpunit/phpunit=${{ matrix.phpunit-versions }}"
35+
- name: Install composer dependencies
36+
run: composer install --prefer-dist --no-progress --no-scripts
2737

2838
- name: PHPUnit tests
29-
run: ~/.composer/vendor/bin/phpunit tests
39+
run: vendor/bin/phpunit tests

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "tests/mock_responses"]
2+
path = tests/mock_responses
3+
url = https://github.com/zerodha/kiteconnect-mocks

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ The Official PHP client for communicating with the [Kite Connect API](https://ki
55
Note: For PHP version < 8.0. You can refer to our [previous version](https://github.com/zerodha/phpkiteconnect/releases/tag/v3.0.0).
66
Kite Connect is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. Execute orders in real time, manage user portfolio and more, with the simple HTTP API collection.
77

8-
[Zerodha Technology](http://zerodha.com) (c) 2021. Licensed under the MIT License.
8+
[Zerodha Technology](http://zerodha.com) (c) 2025. Licensed under the MIT License.
99

1010
## Documentation
1111

@@ -40,7 +40,7 @@ Note: You can refer to our previous version [here](https://github.com/zerodha/ph
4040

4141
// Assuming you have obtained the `request_token`
4242
// after the auth flow redirect by redirecting the
43-
// user to $kite->login_url()
43+
// user to $kite->getLoginURL()
4444
try {
4545
$user = $kite->generateSession("request_token_obtained", "your_api_secret");
4646
echo "Authentication successful. \n";
@@ -80,7 +80,7 @@ Refer to the [PHP client documentation](https://kite.trade/docs/phpkiteconnect/v
8080
## Run unit tests
8181

8282
```
83-
phpunit tests/KiteConnectTest.php
83+
composer test
8484
```
8585

8686
## Generate documentation

composer.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=7.3",
19+
"php": ">=8.0",
2020
"ext-curl": "*",
2121
"ext-json": "*",
2222
"ext-zlib": "*",
23-
"guzzlehttp/guzzle": "^7.2",
24-
"guzzlehttp/psr7": "^1.7"
23+
"guzzlehttp/guzzle": "^7.9",
24+
"guzzlehttp/psr7": "^2.7",
25+
"ratchet/pawl": "^0.4.3",
26+
"react/socket": "^1.16",
27+
"react/stream": "^1.4"
2528
},
2629
"require-dev": {
27-
"friendsofphp/php-cs-fixer": "^2.17",
28-
"phpunit/phpunit": "^9.5",
29-
"vimeo/psalm": "^4.3"
30+
"friendsofphp/php-cs-fixer": "^3.0",
31+
"phpunit/phpunit": "^9.5 || ^10.0",
32+
"vimeo/psalm": "^5.0"
3033
},
3134
"autoload": {
3235
"psr-4": {

examples/kiteconnect_sample.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
// Assuming you have obtained the `request_token`
1111
// after the auth flow redirect by redirecting the
12-
// user to $kite->login_url()
12+
// user to $kite->getLoginURL()
1313
try {
1414
$user = $kite->generateSession("request_token", "secret_key");
1515

examples/ticker_example.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
require_once 'vendor/autoload.php';
4+
5+
use KiteConnect\KiteConnect;
6+
use KiteConnect\KiteTicker;
7+
8+
// Example usage of KiteTicker with the official KiteConnect client
9+
10+
$apiKey = 'your_api_key_here';
11+
$accessToken = 'your_access_token_here';
12+
13+
// Create ticker instance with proper configuration
14+
$ticker = new KiteTicker(
15+
$apiKey,
16+
$accessToken,
17+
30, // timeout
18+
true, // auto-reconnect
19+
true // debug mode
20+
);
21+
22+
// Connection event - fires when WebSocket connects successfully
23+
$ticker->on('connect', function() use ($ticker) {
24+
echo "Connected to KiteTicker WebSocket!\n";
25+
26+
// Subscribe to instruments after connection
27+
$ticker->subscribe([738561], KiteTicker::MODE_FULL);
28+
echo "Subscribed to instrument 738561 (RELIANCE) in FULL mode\n";
29+
});
30+
31+
// Market data event - receives real-time ticks
32+
$ticker->on('ticks', function(array $ticks) {
33+
foreach ($ticks as $tick) {
34+
$token = $tick['instrument_token'];
35+
$price = $tick['last_price'];
36+
$volume = $tick['volume_traded'];
37+
$mode = $tick['mode'];
38+
39+
echo "Token: {$token} | Price: {$price} | Volume: {$volume} | Mode: {$mode}\n";
40+
}
41+
});
42+
43+
// Error event - handles connection and data errors
44+
$ticker->on('error', function(Exception $error) {
45+
echo "WebSocket Error: " . $error->getMessage() . "\n";
46+
});
47+
48+
// Disconnection event - fires when connection is lost
49+
$ticker->on('disconnect', function(int $code, string $reason) {
50+
echo "WebSocket Disconnected: [{$code}] {$reason}\n";
51+
});
52+
53+
// Connection close event - fires when connection closes gracefully
54+
$ticker->on('close', function(int $code, string $reason) {
55+
echo "WebSocket Connection Closed: [{$code}] {$reason}\n";
56+
});
57+
58+
// Reconnection attempt event - fires during auto-reconnect
59+
$ticker->on('reconnect', function(int $attempt) {
60+
echo "Attempting to reconnect... (Attempt #{$attempt})\n";
61+
});
62+
63+
// Max reconnection attempts reached
64+
$ticker->on('noreconnect', function() {
65+
echo "Maximum reconnection attempts reached. Connection abandoned.\n";
66+
});
67+
68+
// Raw message event - receives all WebSocket messages
69+
$ticker->on('message', function(string $payload, bool $isBinary) {
70+
if ($isBinary) {
71+
echo "Binary message received (" . strlen($payload) . " bytes)\n";
72+
} else {
73+
echo "Text message received: " . $payload . "...\n";
74+
}
75+
});
76+
77+
// Order update event - receives order status updates
78+
$ticker->on('order_update', function(array $orderUpdate) {
79+
echo "Order Update Received:\n";
80+
echo "Order ID: {$orderUpdate['order_id']} | Status: {$orderUpdate['status']} | Price: {$orderUpdate['price']} | Quantity: {$orderUpdate['quantity']}\n";
81+
});
82+
83+
echo "Starting KiteTicker WebSocket connection...\n";
84+
85+
// Start the WebSocket connection
86+
$ticker->connect();

phpunit.xml.dist

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit backupGlobals="false"
3-
backupStaticAttributes="false"
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
4+
backupGlobals="false"
45
bootstrap="vendor/autoload.php"
56
colors="true"
67
convertErrorsToExceptions="true"
78
convertNoticesToExceptions="true"
89
convertWarningsToExceptions="true"
10+
verbose="true"
911
processIsolation="false"
1012
stopOnFailure="false"
11-
verbose="true"
1213
>
1314
<testsuites>
1415
<testsuite name="KiteConnect Test Suite">
1516
<directory>tests</directory>
1617
</testsuite>
1718
</testsuites>
18-
<coverage>
19-
<include>
20-
<directory suffix=".php">./src</directory>
21-
</include>
22-
<report>
23-
<html outputDirectory="build/coverage"/>
24-
<text outputFile="build/coverage.txt"/>
25-
<clover outputFile="build/logs/clover.xml"/>
26-
</report>
27-
</coverage>
28-
<logging>
29-
<junit outputFile="build/report.junit.xml"/>
30-
</logging>
3119
</phpunit>

psalm.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0"?>
22
<psalm
3+
phpVersion="8.0"
34
errorLevel="4"
45
findUnusedVariablesAndParams="true"
56
resolveFromConfigFile="true"

src/KiteConnect.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use KiteConnect\Exception\OrderException;
1818
use KiteConnect\Exception\PermissionException;
1919
use KiteConnect\Exception\TokenException;
20-
use phpDocumentor\Reflection\Types\Mixed_;
2120
use stdClass;
2221

2322
/**
@@ -49,7 +48,7 @@
4948
* use one instance to manage many users.
5049
* Hence, in your web application, typically:
5150
* - You will initialise an instance of the Kite client
52-
* - Redirect the user to the `login_url()`
51+
* - Redirect the user to the `getLoginURL()`
5352
* - At the redirect url endpoint, obtain the
5453
* `request_token` from the query parameters
5554
* - Initialise a new instance of Kite client,
@@ -209,6 +208,9 @@ class KiteConnect
209208
/** @var Closure */
210209
private $sessionHook;
211210

211+
/** @var \GuzzleHttp\Client|null */
212+
private ?\GuzzleHttp\Client $guzzleClient;
213+
212214
/**
213215
* Initialise a new Kite Connect client instance.
214216
*
@@ -390,7 +392,7 @@ public function renewAccessToken(string $refreshToken, string $apiSecret): array
390392
* @throws PermissionException
391393
* @throws TokenException
392394
*/
393-
public function invalidateRefreshToken(string $refreshToken): Mixed_
395+
public function invalidateRefreshToken(string $refreshToken): mixed
394396
{
395397
return $this->delete("api.token.invalidate", [
396398
"refresh_token" => $refreshToken,
@@ -499,7 +501,7 @@ public function placeOrder(string $variety, array $params)
499501
* @throws PermissionException
500502
* @throws TokenException
501503
*/
502-
public function modifyOrder(string $variety, string $orderId, array $params): Mixed_
504+
public function modifyOrder(string $variety, string $orderId, array $params): mixed
503505
{
504506
$params["variety"] = $variety;
505507
$params["order_id"] = $orderId;
@@ -1537,7 +1539,7 @@ private function request(string $route, string $method, array $params, string $h
15371539
* @param array|null $params Array of key=>value request parameters.
15381540
* @return array Returns an array with response "headers" and "body".
15391541
*/
1540-
private function guzzle(string $url, string $method, ?array $headers, $params = null, $guzzleClient = null): array
1542+
private function guzzle(string $url, string $method, ?array $headers, $params = null, ?\GuzzleHttp\Client $guzzleClient = null): array
15411543
{
15421544
// set header to Guzzle http client
15431545
// mock patching isn't allowed in PHP

0 commit comments

Comments
 (0)