Skip to content

Commit fb8e821

Browse files
authored
Merge pull request #17 from zerodha/refactor
Release: v4.0.0
2 parents e282f42 + 11b2e48 commit fb8e821

Some content is hidden

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

47 files changed

+5291
-1519
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.idea
2+
.php_cs
3+
.php_cs.cache
4+
.phpunit.result.cache
5+
build
6+
composer.lock
7+
coverage
8+
docs
9+
phpunit.xml
10+
psalm.xml
11+
vendor
12+
index.php

CHANGELOG.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
New features
2-
=============
1+
# Changelog
2+
3+
All notable changes to `phpkiteconnect` will be documented in this file.
4+
5+
## 1.0.0 - 202X-XX-XX
6+
7+
- initial release
8+
#### New Features
39
- method: `getProfile`
410
- method: `getOHLC`
511
- method: `getLTP`
@@ -42,9 +48,12 @@ API method name changes
4248

4349
Params and other changes
4450
========================
51+
- `placeOrder` method takes all the params as array including `variety`
52+
- `modifyOrder` method takes all the params as array including `variety` and `order_id`
53+
- `cancelOrder` method takes all the params as array
4554
- `convertPosition` method takes all the params as array
4655
- `getHistoricalData` method takes all the params as array
47-
- [Changes in `generateSession` response structure](https://kite.trade/docs/connect/v3/user/#response-attributes)
56+
- [Changes in `requestAccessToken` response structure](https://kite.trade/docs/connect/v3/user/#response-attributes)
4857
- [Changes in `getPositions` response structure](https://kite.trade/docs/connect/v3/portfolio/#response-attributes_1)
4958
- [Changes in `getQuote` response structure](https://kite.trade/docs/connect/v3/market-quotes/#retrieving-full-market-quotes)
5059
- [Changes in `placeOrder` params](https://kite.trade/docs/connect/v3/orders/#bracket-order-bo-parameters)

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) [Zerodha Technology](http://zerodha.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
# The Kite Connect API PHP client - v3
2-
The official PHP client for communicating with the [Kite Connect API](https://kite.trade).
2+
The Official PHP client for communicating with the [Kite Connect API](https://kite.trade).
33

44
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.
55

6-
[Zerodha Technology](http://zerodha.com) (c) 2018. Licensed under the MIT License.
6+
[Zerodha Technology](http://zerodha.com) (c) 2021. Licensed under the MIT License.
77

88
## Documentation
99
- [PHP client documentation](https://kite.trade/docs/phpkiteconnect/v3)
1010
- [Kite Connect HTTP API documentation](https://kite.trade/docs/connect/v3)
1111

1212
## Installing
13-
Download `kiteconnect.php` and `include()` it in your application.
13+
### Requirements
14+
1. Install [PHP](https://www.php.net/manual/en/install.php) version 7.3 or higher.
15+
2. Install [Composer](https://getcomposer.org/download/), which is used to install PHP packages.
16+
17+
You can install the package via composer:
18+
```bash
19+
composer require zerodha/phpkiteconnect
20+
```
1421

1522
## Usage
1623
```php
1724
<?php
18-
include dirname(__FILE__)."/kiteconnect.php";
25+
require_once __DIR__ . '/vendor/autoload.php';
26+
27+
use KiteConnect\KiteConnect;
1928

2029
// Initialise.
2130
$kite = new KiteConnect("your_api_key");
@@ -25,10 +34,8 @@ Download `kiteconnect.php` and `include()` it in your application.
2534
// user to $kite->login_url()
2635
try {
2736
$user = $kite->generateSession("request_token_obtained", "your_api_secret");
28-
2937
echo "Authentication successful. \n";
3038
print_r($user);
31-
3239
$kite->setAccessToken($user->access_token);
3340
} catch(Exception $e) {
3441
echo "Authentication failed: ".$e->getMessage();
@@ -42,7 +49,7 @@ Download `kiteconnect.php` and `include()` it in your application.
4249
print_r($kite->getPositions());
4350

4451
// Place order.
45-
$o = $kite->placeOrder("regular", [
52+
$order = $kite->placeOrder("regular", [
4653
"tradingsymbol" => "INFY",
4754
"exchange" => "NSE",
4855
"quantity" => 1,
@@ -51,12 +58,20 @@ Download `kiteconnect.php` and `include()` it in your application.
5158
"product" => "NRML"
5259
]);
5360

54-
echo "Order id is ".$o->order_id;
61+
echo "Order id is ".$order->order_id;
5562
?>
5663
```
5764

65+
## Examples
66+
67+
Check [examples folder](https://github.com/zerodha/phpkiteconnect/tree/master/examples) for more examples.
68+
5869
Refer to the [PHP client documentation](https://kite.trade/docs/phpkiteconnect/v3) for the complete list of supported methods.
5970

60-
## Changelog
61-
[Check CHANGELOG.md](CHANGELOG.md)
71+
## Run unit tests
72+
```
73+
phpunit tests/KiteConnectTest.php
74+
```
6275

76+
## Changelog
77+
[Check CHANGELOG.md](CHANGELOG.md)

composer.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "zerodha/phpkiteconnect",
3+
"description": "The PHP client library for the Kite Connect trading APIs Resources",
4+
"type": "library",
5+
"keywords": [
6+
"zerodha",
7+
"phpkiteconnect"
8+
],
9+
"homepage": "https://github.com/zerodha/phpkiteconnect",
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "zerodha",
14+
"email": "[email protected]",
15+
"role": "Owner"
16+
}
17+
],
18+
"require": {
19+
"php": ">=7.3",
20+
"ext-curl": "*",
21+
"ext-json": "*",
22+
"ext-zlib": "*",
23+
"guzzlehttp/guzzle": "^7.2",
24+
"guzzlehttp/psr7": "^1.7"
25+
},
26+
"require-dev": {
27+
"friendsofphp/php-cs-fixer": "^2.17",
28+
"phpunit/phpunit": "^9.5",
29+
"vimeo/psalm": "^4.3"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"KiteConnect\\": "src/"
34+
}
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"KiteConnect\\Tests\\": "tests"
39+
}
40+
},
41+
"scripts": {
42+
"psalm": "vendor/bin/psalm",
43+
"test": "vendor/bin/phpunit",
44+
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
45+
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
46+
},
47+
"config": {
48+
"sort-packages": true
49+
},
50+
"minimum-stability": "dev",
51+
"prefer-stable": true
52+
}

examples/kiteconnect_sample.php

Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,80 @@
11
<?php
2-
include dirname(__FILE__)."/kiteconnect.php";
3-
4-
// Initialise.
5-
$kite = new KiteConnect("your_api_key");
6-
7-
// Assuming you have obtained the `request_token`
8-
// after the auth flow redirect by redirecting the
9-
// user to $kite->login_url()
10-
try {
11-
$user = $kite->generateSession("request_token_obtained", "your_api_secret");
12-
13-
echo "Authentication successful. \n";
14-
print_r($user);
15-
16-
$kite->setAccessToken($user->access_token);
17-
} catch(Exception $e) {
18-
echo "Authentication failed: ".$e->getMessage();
19-
throw $e;
20-
}
21-
22-
echo $user->user_id." has logged in";
23-
24-
// Get the list of positions.
25-
echo "Positions: \n";
26-
print_r($kite->getPositions());
27-
28-
// Get the list of holdings.
29-
echo "Holdings: \n";
30-
print_r($kite->getHoldings());
31-
32-
// Retrieve quote and market depth for list of instruments.
33-
echo "Quote: \n";
34-
print_r($kite->getQuote(["NSE:INFY", "NSE:SBIN"]));
35-
36-
// Place order.
37-
$order_id = $kite->placeOrder("regular", [
38-
"tradingsymbol" => "INFY",
39-
"exchange" => "NSE",
40-
"quantity" => 1,
41-
"transaction_type" => "BUY",
42-
"order_type" => "MARKET",
43-
"product" => "NRML"
44-
])["order_id"];
45-
46-
echo "Order id is ".$order_id;
47-
48-
// fetch order margin
49-
$order_param = array(array("exchange" => "NSE",
50-
"tradingsymbol" => "INFY",
51-
"transaction_type" => $kite::TRANSACTION_TYPE_BUY,
52-
"variety" => $kite::VARIETY_REGULAR,
53-
"product" => $kite::PRODUCT_CNC,
54-
"order_type" => $kite::ORDER_TYPE_MARKET,
55-
"quantity" => 1
56-
),);
57-
print_r($kite->orderMargins($order_param));
2+
3+
require_once __DIR__ . '/vendor/autoload.php';
4+
5+
use KiteConnect\KiteConnect;
6+
7+
// Initialise.
8+
$kite = new KiteConnect("api_key");
9+
10+
// Assuming you have obtained the `request_token`
11+
// after the auth flow redirect by redirecting the
12+
// user to $kite->login_url()
13+
try {
14+
$user = $kite->generateSession("request_token", "secret_key");
15+
16+
echo "Authentication successful. \n";
17+
print_r($user);
18+
19+
$kite->setAccessToken($user->access_token);
20+
} catch (Exception $e) {
21+
echo "Authentication failed: " . $e->getMessage();
22+
23+
throw $e;
24+
}
25+
26+
echo $user->user_id . " has logged in";
27+
28+
// Get the list of positions.
29+
echo "Positions: \n";
30+
print_r($kite->getPositions());
31+
32+
// Get the list of holdings.
33+
echo "Holdings: \n";
34+
print_r($kite->getHoldings());
35+
36+
// Retrieve quote and market depth for list of instruments.
37+
echo "Quote: \n";
38+
print_r($kite->getQuote(["NSE:INFY", "NSE:SBIN"]));
39+
40+
// Place order.
41+
$order = $kite->placeOrder("regular", [
42+
"tradingsymbol" => "INFY",
43+
"exchange" => "NSE",
44+
"quantity" => 1,
45+
"transaction_type" => "BUY",
46+
"order_type" => "MARKET",
47+
"product" => "CNC",
48+
]);
49+
50+
echo "Order id is " . $order->order_id;
51+
52+
// fetch order margin
53+
$order_param = [["exchange" => "NSE",
54+
"tradingsymbol" => "INFY",
55+
"transaction_type" => $kite::TRANSACTION_TYPE_BUY,
56+
"variety" => $kite::VARIETY_REGULAR,
57+
"product" => $kite::PRODUCT_CNC,
58+
"order_type" => $kite::ORDER_TYPE_MARKET,
59+
"quantity" => 1,
60+
],];
61+
62+
print_r($kite->orderMargins($order_param));
63+
64+
$place_GTT = $kite->placeGTT([
65+
"trigger_type" => $kite::GTT_TYPE_SINGLE,
66+
"tradingsymbol" => "TATAMOTORS",
67+
"exchange" => "NSE",
68+
"trigger_values" => array(310),
69+
"last_price" => 315,
70+
"orders" => array([
71+
"transaction_type" => $kite::TRANSACTION_TYPE_BUY,
72+
"quantity" => 1,
73+
"product" => $kite::PRODUCT_CNC,
74+
"order_type" => $kite::ORDER_TYPE_LIMIT,
75+
"price" => 314
76+
])
77+
]);
78+
echo "Trigger id is ".$place_GTT->trigger_id;
79+
5880
?>

0 commit comments

Comments
 (0)