-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBorsdataAPI.php
More file actions
559 lines (460 loc) · 20 KB
/
BorsdataAPI.php
File metadata and controls
559 lines (460 loc) · 20 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php
/**
* @author ReineW
* @license MIT
* @link https://github.com/reinew/borsdata-api-php
*
* This class is a PHP wrapper for easily interacting with Borsdata API.
*
* This class have not been thoroughly tested under all conditions.
* The creator cannot guarantee or imply reliability, serviceability, or function of this class.
* All code contained herein are provided to you “AS IS” without any warranties of any kind.
*/
class BorsdataAPI {
private $BASE_URL = 'https://apiservice.borsdata.se';
private $VERSION = 'v1';
private $key; // API key.
private $SLEEP = 0.11; // Max 100 requests allowed per 10s.
/** Getting the API key from the .env file. */
public function __construct() {
try {
$envFile = __DIR__ . '/.env';
if (!file_exists($envFile)) {
throw new Exception("The .env file is missing, please create one and add your API_KEY.\n");
}
$dotEnv = parse_ini_file($envFile);
$this->key = $dotEnv['API_KEY'] ?? throw new Exception("API_KEY key is missing in .env file.\n");
} catch (Exception $error) {
echo $error->getMessage();
die();
}
}
/** This function calls Borsdata API.
* @param string $requestUrl API request URL.
* @return object array with JSON data.
* @throws error API error.
*/
function getDataFromApi(string $requestUrl) {
$url = $this->BASE_URL . '/' . $this->VERSION . '/' . $requestUrl;
$context = stream_context_create([
'http' => [
'ignore_errors' => true
]
]);
try {
$response = file_get_contents($url, false, $context);
$statusCode = explode(' ', $http_response_header[0])[1];
$responseText = explode(' ', $http_response_header[0])[2];
if ($statusCode == '200') {
$result = json_decode($response, true);
sleep($this->SLEEP);
return $result;
} elseif ($statusCode == '418') {
throw new Exception("API request failed with HTTP status code 418 (No global access)\n");
} else {
throw new Exception("API request failed with HTTP status code $statusCode ($responseText)\n");
}
} catch (Exception $error) {
echo "API request failed: " . $error->getMessage() . "\n";
die();
}
}
/** This function returns all nordic instruments or metadata. \
* \
* Choose one of the following API options: \
* 'instruments' - Returns all instruments. \
* 'branches' - Returns all branches. \
* 'countries' - Returns all countries for nordic. \
* 'markets' - Returns all markets. \
* 'sectors' - Returns all sectors. \
* 'translationmetadata' - Returns language translations for bransch, sector or country.
*
* @param string $option API option.
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Instruments
*/
function getAllInstruments(string $instrumentsOption) {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "$instrumentsOption?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns Holdings data in the nordic for a list of instruments. (Require Pro+) \
* \
* Choose one of the following API options: \
* 'insider' - Returns holdings insider. \
* 'shorts' - Returns holdings short. \
* 'buyback' - Returns holdings buyback.
*
* @param string $holdingsOption API option.
* @param string $instList Comma separated list of instrument id's. (Max 50) (Get all different id's with the getAllInstruments('instruments') function.)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Holdings
*/
function getHoldings(string $holdingsOption, string $instList) {
$queryParams = [
'authKey' => $this->key,
'instList' => $instList,
];
$requestUrl = "holdings/$holdingsOption?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns all global instruments. (Require Pro+)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Instruments
*/
function getAllGlobalInstruments() {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/global?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns all updated nordic instruments.
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Instruments
*/
function getAllUpdatedInstruments() {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/updated?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns descriptions for a list of instruments.
* @param string $instList Comma separated list of instrument id's. (Max 50) (Get all different id's with the getAllInstruments('instruments') function.)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Instruments
*/
function getInstrumentDescriptions(string $instList) {
$queryParams = [
'authKey' => $this->key,
'instList' => $instList,
];
$requestUrl = "instruments/description?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns calendar data for the nordic market.
* @param string $calendarOption Calendar option. (report, dividend)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Calendar
*/
function getCalendar(string $calendarOption, string $instList) {
$queryParams = [
'authKey' => $this->key,
'instList' => $instList,
];
$requestUrl = "instruments/$calendarOption/calendar?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns kpi history for the nordic market.
* @param int $insId Instrument id. (Get all different id's with the "getAllInstruments('instruments')" function.)
* @param int $kpiId Kpi id. (Get all different id's with the "getKpiMetadata()" function.)
* @param string $reportType Report type. (year, r12, quarter)
* @param string $priceType Price type. (low, mean or high)
* @param int $maxCount Max number of results returned. (Max Year=20, R12&Quarter=40) (optional)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/KPI-History
*/
function getKpiHistory(int $insId, int $kpiId, string $reportType, string $priceType, int $maxCount = null) {
$queryParams = [
'authKey' => $this->key,
];
if ($maxCount !== null) {
$queryParams['maxCount'] = $maxCount;
}
$requestUrl = "instruments/$insId/kpis/$kpiId/$reportType/$priceType/history?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns a kpi summary list for one instrument in the nordic market.
* @param int $insId Instrument id. (Get all different id's with the "getAllInstruments('instruments')" function.)
* @param string $reportType Report type. (year, r12, quarter)
* @param int $maxCount Max number of results returned. (optional)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/KPI-History
*/
function getKpiSummary(int $insId, string $reportType, int $maxCount = null) {
$queryParams = [
'authKey' => $this->key,
];
if ($maxCount !== null) {
$queryParams['maxCount'] = $maxCount;
}
$requestUrl = "instruments/$insId/kpis/$reportType/summary?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns kpi history for a list of instruments in the nordic market.
* @param int $kpiId Kpi id. (Get all different id's with the "getKpiMetadata()" function.)
* @param string $reportType Report type. (year, r12, quarter)
* @param string $priceType Price type. (low, mean or high)
* @param string $instList Comma separated list of instrument id's. (Max 50) (Get all different id's with the getAllInstruments('instruments') function.)
* @param int $maxCount Max number of results returned. (Max Year=20, R12&Quarter=40) (optional)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/KPI-History
*/
function getHistoricalKpis(int $kpiId, string $reportType, string $priceType, string $instList, int $maxCount = null) {
$queryParams = [
'authKey' => $this->key,
'instList' => $instList,
];
if ($maxCount !== null) {
$queryParams['maxCount'] = $maxCount;
}
$requestUrl = "instruments/kpis/$kpiId/$reportType/$priceType/history?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns kpi data for one instrument in the nordic market.
* @param int $insId Instrument id. (Get all different id's with the getAllInstruments('instruments') function.)
* @param int $kpiId Kpi id. (Get all different id's with the "getKpiMetadata()" function.)
* @param string $calcGroup Kpi calculation group. Mainly based on time.
* @param string $calc Kpi calculation.
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/KPI-Screener
*/
function getKpidataForOneInstrument(int $insId, int $kpiId, string $calcGroup, string $calc) {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/$insId/kpis/$kpiId/$calcGroup/$calc?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns kpi data for all instruments in the nordic market.
* @param int $kpiId Kpi id. (Get all different id's with the "getKpiMetadata()" function.)
* @param string $calcGroup Kpi calculation group. Mainly based on time.
* @param string $calc Kpi calculation.
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/KPI-Screener
*/
function getKpidataForAllInstruments(int $kpiId, string $calcGroup, string $calc) {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/kpis/$kpiId/$calcGroup/$calc?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns kpi data for all global instruments. (Require Pro+)
* @param int $kpiId Kpi id. (Get all different id's with the "getKpiMetadata()" function.)
* @param string $calcGroup Kpi calculation group. Mainly based on time.
* @param string $calc Kpi calculation.
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/KPI-Screener
*/
function getKpidataForAllGlobalInstruments(int $kpiId, string $calcGroup, string $calc) {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/global/kpis/$kpiId/$calcGroup/$calc?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns nordic kpi last updated calculation dateTime.
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/KPI-Screener
*/
function getKpiUpdated() {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/kpis/updated?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns kpi metadata.
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/KPI-Screener
*/
function getKpiMetadata() {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/kpis/metadata?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns reports for one instrument with one report type for the nordic market.
* @param int $insId Instrument id. (Get all different id's with the "getAllInstruments('instruments')" function.)
* @param string $reportType Report type. (year, r12, quarter)
* @param int $maxCount Max number of results returned. (Max Year=20, R12&Quarter=40) (optional)
* @param int $original Get original report data currency. (optional) {0, 1}
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Reports
*/
function getReportsByType(int $insId, string $reportType, int $maxCount = null, int $original = null) {
$queryParams = [
'authKey' => $this->key,
];
if ($maxCount !== null) {
$queryParams['maxCount'] = $maxCount;
}
if ($original !== null) {
$queryParams['original'] = $original;
}
$requestUrl = "instruments/$insId/reports/$reportType?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns reports for one instrument with all report types included. (year, r12, quarter)
* @param int $insId Instrument id. (Get all different id's with the "getAllInstruments('instruments')" function.)
* @param int $maxYearCount Max number of year reports returned. (10 year default, max 20) (Optional)
* @param int $maxR12QCount Max number of r12 and quarter reports returned. (10 default, max 40) (Optional)
* @param int $original Get original report data currency. (optional) {0, 1}
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Reports
*/
// function getReportsForAllTypes($insId, $maxYearCount, $maxR12QCount)
function getReportsForAllTypes(int $insId, int $maxYearCount = null, int $maxR12QCount = null, int $original = null) {
$queryParams = [
'authKey' => $this->key,
];
if ($maxYearCount !== null) {
$queryParams['maxYearCount'] = $maxYearCount;
}
if ($maxR12QCount !== null) {
$queryParams['maxR12QCount'] = $maxR12QCount;
}
if ($original !== null) {
$queryParams['original'] = $original;
}
$requestUrl = "instruments/$insId/reports?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns report metadata for the nordic market.
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Reports
*/
function getReportsMetadata() {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/reports/metadata?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns reports for list of instruments with all report types included. (year, r12, quarter)
* @param string $instList Comma separated list of instrument id's. (Max 50) (Get all different id's with the "getAllInstruments('instruments')" function.)
* @param int $maxYearCount Max number of year reports returned. (10 year default, max 20) (Optional)
* @param int $maxR12QCount Max number of r12 and quarter reports returned. (10 default, max 40) (Optional)
* @param int $original Get original report data currency. (optional) {0, 1}
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Reports
*/
function getAllReports(string $instList, int $maxYearCount = null, int $maxR12QCount = null, int $original = null) {
$queryParams = [
'authKey' => $this->key,
'instList' => $instList,
];
if ($maxYearCount !== null) {
$queryParams['maxYearCount'] = $maxYearCount;
}
if ($maxR12QCount !== null) {
$queryParams['maxR12QCount'] = $maxR12QCount;
}
if ($original !== null) {
$queryParams['original'] = $original;
}
$requestUrl = "instruments/reports?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns stockprices for one instrument between two dates for the nordic market.
* @param int $insId Instrument id. (Get all different id's with the "getAllInstruments('instruments')" function.)
* @param string $from From date. (YYYY-MM-DD) (optional)
* @param string $to To date. (YYYY-MM-DD) (optional)
* @param int $maxCount Max number of results returned. (Max 20) (optional)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Stockprice
*/
function getStockpricesForInstrument(int $insId, string $from = null, string $to = null, int $maxCount = null) {
$queryParams = [
'authKey' => $this->key,
];
if ($from !== null) {
$queryParams['from'] = $from;
}
if ($to !== null) {
$queryParams['to'] = $to;
}
if ($maxCount !== null) {
$queryParams['maxCount'] = $maxCount;
}
$requestUrl = "instruments/$insId/stockprices?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns stockprices for a list of instruments between two dates for the nordic market.
* @param string $instList Comma separated list of instrument id's. (Max 50) (Get all different id's with the "getAllInstruments('instruments')" function.)
* @param string $from From date. (YYYY-MM-DD) (optional)
* @param string $to To date. (YYYY-MM-DD) (optional)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Stockprice
*/
function getStockPricesForListOfInstruments(string $instList, string $from = null, string $to = null) {
$queryParams = [
'authKey' => $this->key,
'instList' => $instList,
];
if ($from !== null) {
$queryParams['from'] = $from;
}
if ($to !== null) {
$queryParams['to'] = $to;
}
$requestUrl = "instruments/stockprices?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns last stockprices for all nordic instruments.
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Stockprice
*/
function getLastStockprices() {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/stockprices/last?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns last/latest stockprices for all global instruments. Only Global(Pro+)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Stockprice
*/
function getLastGlobalStockprices() {
$queryParams = [
'authKey' => $this->key,
];
$requestUrl = "instruments/stockprices/global/last?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns one stock price for each nordic instrument on a specific date.
* @param string $date Date. (YYYY-MM-DD)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Stockprice
*/
function getStockpricesForDate(string $date) {
$queryParams = [
'authKey' => $this->key,
'date' => $date,
];
$requestUrl = "instruments/stockprices/date?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns one stock price for each global instrument on a specific date. Only Global(Pro+)
* @param string $date Date. (YYYY-MM-DD)
* @return object array with JSON data.
* @link https://github.com/Borsdata-Sweden/API/wiki/Stockprice
*/
function getGlobalStockpricesForDate(string $date) {
$queryParams = [
'authKey' => $this->key,
'date' => $date,
];
$requestUrl = "instruments/stockprices/global/date?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
/** This function returns stock splits for all nordic instruments. Max 1 year history.
* @return object array with JSON data.
*/
function getStocksplits(string $from = null) {
$queryParams = [
'authKey' => $this->key,
];
if ($from !== null) {
$queryParams['from'] = $from;
}
$requestUrl = "instruments/StockSplits?" . http_build_query($queryParams);
return $this->getDataFromApi($requestUrl);
}
}