-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathhaloapi.class.php
More file actions
621 lines (523 loc) · 17.9 KB
/
haloapi.class.php
File metadata and controls
621 lines (523 loc) · 17.9 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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<?php
/**
*
* PHP-HaloAPI
* v 1.0.3-beta
*
* This class has for purpose to simplify the work of PHP developers who wants to use the official (beta) Halo 5 API.
*
* Author: Gaspard Rosay - @BananasSplitter
* Date: 04.11.15
* WTPFL Licence
*
*/
class haloapi
{
const BASE_URL = "https://www.haloapi.com/"; // Base url for API, may change on day...
private $sApiKey = ""; // Will contain the API key
private $sTitle = ""; // Correspond to the game title - for now only Halo 5 (h5)
private $aPlayerNames = array(); // List of users (functions may use only the first user)
private $lastHeaders = array(); // Array of parsed headers from the last API call
public $lastApiVersion = ""; // X-343-Version header from the last API call
public static $queryLimit = 10; // The number of queries that you are allowed to perform within the alloted time window.
public static $queryWindowSecs = 10; // The time window on which queries are bound, in seconds.
private static $queryCount = 0;
private static $queryWindowStartTime;
/**
* @name __construct
*
* Initialize the class
*
* @param $aPlayerNames: an array containing list of players
* @param $sTitle: the title concerned by the API (for now, only h5 is valid) - default: h5
*/
function __construct($sApiKey, $aPlayerNames, $sTitle = "h5"){
$this->sApiKey = $sApiKey;
$this->aPlayerNames = $aPlayerNames;
$this->sTitle = $sTitle;
}
### Global functions
/**
* @name callAPI
*
* Make curl request to the API
*
* @param $sUrl: url to use in API call
*
* @return $response: the API response
*/
private function callAPI($sUrl){
self::throttle();
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => true,
CURLOPT_ENCODING => "gzip",
CURLOPT_URL => $sUrl,
CURLOPT_USERAGENT => 'PHP-HaloAPI',
CURLOPT_HTTPHEADER => array(
'Ocp-Apim-Subscription-Key: '.$this->sApiKey
)
));
$resp = curl_exec($ch);
if(!$resp){
die('Error in API call: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}
else{
// Then, after your curl_exec call:
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($resp, 0, $header_size);
$body = substr($resp, $header_size);
}
curl_close($ch);
// Parse the headers and store them in the lastHeaders class property
$this->lastHeaders = $this->getHeaders($header);
// Keep track of the API version in the lastApiVersion class property
if(isset($this->lastHeaders['X-343-Version'])){
$this->lastApiVersion = $this->lastHeaders['X-343-Version'];
}
return array('header' => $header, 'body' => $body);
}
/**
* @name throttle
*
* Throttle API calls using the $queryLimit class property
**
* @return null
*/
private static function throttle(){
self::$queryCount++;
if(self::$queryWindowStartTime === null){
self::$queryWindowStartTime = new DateTime();
}
if(self::$queryCount > self::$queryLimit){
$now = new DateTime();
$diffSec = $now->getTimestamp() - self::$queryWindowStartTime->getTimestamp();
// If we've exceeded the query count, and we still are within the query window, then wait.
if($diffSec < self::$queryWindowSecs){
sleep(self::$queryWindowSecs - $diffSec + 1);
}
// Then, once we've waited, if necessary, we'll assume that we're beyond the query window, so we'll start our throttling over.
self::$queryWindowStartTime = new DateTime();
self::$queryCount = 1;
}
}
/**
* @name getHeaders
*
* Return headers information from curl header
*
* @param $sHeader: header returned by curl
*
* @return $headers: array containing all headers infos
*/
private function getHeaders($sHeader){
$headers = array();
$header_text = substr($sHeader, 0, strpos($sHeader, "\r\n\r\n"));
foreach(explode("\r\n", $header_text) as $i => $line){
if($i === 0){
$headers['http_code'] = $line;
}
else{
list($key, $value) = explode(': ', $line);
$headers[$key] = $value;
}
}
return $headers;
}
/**
* @name $this->decodeJson
*
* Return decoded string from json - return an error if json isn't correct
*
* @param $json: the encoded json string
*
* @return $json: the json string once decoded
*/
private function decodeJson($json){
// utf8_encode the json string only if the response charset was not set to utf-8 already
if(!isset($this->lastHeaders['Content-Type']) || $this->lastHeaders['Content-Type'] != 'application/json; charset=utf-8'){
$json = utf8_encode($json);
}
$json = iconv('UTF-8', 'UTF-8//IGNORE', $json);
$json = json_decode($json,1);
if(json_last_error() === JSON_ERROR_NONE){
return $json;
}
else{
return "ERROR JSON(".json_last_error()."): ".json_last_error_msg();
}
}
###
### Profile part
/**
* @name getEmblem
*
* Return the url of player's emblem img
*
* @param $sSize: size wanted - default: null
*
* @return $aHeader['location']: url of the img
*/
public function getEmblem($sSize = null){
$sUrl = self::BASE_URL."profile/".$this->sTitle."/profiles/".$this->aPlayerNames[0]."/emblem";
if(!is_null($sSize)){
$sUrl.= "?size=".$sSize;
}
$response = $this->callAPI($sUrl);
$location = isset($this->lastHeaders['Location']) ? $this->lastHeaders['Location'] : false;
return $location;
}
/**
* @name getSpartanImg
*
* Return the url of player's spartan img
*
* @param $sSize: size wanted - default: null
*
* @return $aHeader['location']: url of the img
*/
public function getSpartanImg($sSize = null){
$sUrl = self::BASE_URL."profile/".$this->sTitle."/profiles/".$this->aPlayerNames[0]."/spartan";
if(!is_null($sSize)){
$sUrl.= "?size=".$sSize;
}
$response = $this->callAPI($sUrl);
$location = isset($this->lastHeaders['Location']) ? $this->lastHeaders['Location'] : false;
return $location;
}
### End of profile part
### Metadata part
/**
* @name getCampaignMissions
*
* Return information of all campaign missions
**
* @return $oJson: json object containing campaign informations
*/
public function getCampaignMissions(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/campaign-missions";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getCommendations
*
* Return information about all commendations
**
* @return $oJson: json object containing commendations data
*/
public function getCommendations(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/commendations";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getCSRDesignations
*
* Return information about all CSR Designations
**
* @return $oJson: json object containing csr designations data
*/
public function getCSRDesignations(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/csr-designations";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getEnemies
*
* Return information about all enemies (IA)
**
* @return $oJson: json object containing enemies data
*/
public function getEnemies(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/enemies";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getFlexibleStats
*
* Return information about flexible stats
**
* @return $oJson: json object containing flexible stats data
*/
public function getFlexibleStats(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/flexible-stats";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getGameBaseVariants
*
* Return information about game base variants
**
* @return $oJson: json object containing game base variants data
*/
public function getGameBaseVariants(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/game-base-variants";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getGameVariantData
*
* Return information about the given game variant
*
* @param $sPackId: ID of the game variant wanted
*
* @return $oJson: json object containing game variant data
*/
public function getGameVariantData($sVariantId){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/game-variants/".$sVariantId;
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getImpulses
*
* Return information about impulses
**
* @return $oJson: json object containing impulses data
*/
public function getImpulses(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/impulses";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getMapVariantData
*
* Return information about the givent map variant
*
* @param $sVariantId: the id of the map variant wanted
*
* @return $oJson: json object containing datas of map variant
*/
public function getMapVariantData($sVariantId){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/map-variant/".$sVariantId;
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getMaps
*
* Return information about maps
**
* @return $oJson: json object containing maps data
*/
public function getMaps(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/maps";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getMedals
*
* Return information about medals
**
* @return $oJson: json object containing medals data
*/
public function getMedals(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/medals";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getPlaylists
*
* Return information about playlists
**
* @return $oJson: json object containing playlists data
*/
public function getPlaylists(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/playlists";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getRequisitionPack
*
* Return information about the given requisition pack
*
* @param $sPackId: ID of the requisition pack wanted
**
* @return $oJson: json object containing requisition pack data
*/
public function getRequisitionPack($sPackId){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/requisition-packs/".$sPackId;
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getRequisition
*
* Return information about the given requisition
*
* @param $sRequisitionId: ID of the requisition wanted
*
* @return $oJson: json object containing requisition data
*/
public function getRequisition($sRequisitionId){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/requisitions/".$sRequisitionId;
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getSkulls
*
* Return information about skulls
**
* @return $oJson: json object containing skulls data
*/
public function getSkulls(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/skulls";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getSeasons
*
* Return information about seasons
**
* @return $oJson: json object containing seasons data
*/
public function getSeasons(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/seasons";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getSpartanRanks
*
* Return information about spartan ranks
**
* @return $oJson: json object containing spartan ranks data
*/
public function getSpartanRanks(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/spartan-ranks";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getTeamColors
*
* Return information about team colors
**
* @return $oJson: json object containing team colors data
*/
public function getTeamColors(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/team-colors";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getVehicles
*
* Return information about vehicles
**
* @return $oJson: json object containing vehicles data
*/
public function getVehicles(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/vehicles";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getWeapons
*
* Return information about weapons
**
* @return $oJson: json object containing weapons data
*/
public function getWeapons(){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/weapons";
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getMetadata
*
* Return information about given metadata
* Can be used instead of calling direct function
*
* @param $sMetadata name of metadata wanted
* @param $sId optional ID
*
* @return $oJson: json object containing weapons data
*/
public function getMetadata($sMetadata, $sId = null){
$sUrl = self::BASE_URL."metadata/".$this->sTitle."/metadata/".$sMetadata.(!is_null($sId) ? "/".$sId : null);
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
### End metadate part
### Stats part
/**
* @name getPlayerMatches
*
* Return a list of all last matches of player
*
* @param $aParams: array - default: null
* 'modes': the id of the mode wanted - if not set, all modes are loaded - separate modes by coma
* 'start': the id of first element to return - if not set or set to 0, first match will be sent
* 'count': the number of elements to return - if not set return 25
*
* @return $oJson: json object containing all matches datas
*/
public function getPlayerMatches($aParams = array()){
$sUrl = self::BASE_URL."stats/".$this->sTitle."/players/".$this->aPlayerNames[0]."/matches";
$i = 0;
if(isset($aParams['modes']) && !is_null($aParams['modes'])){
$sUrl .= ($i == 0 ? "?" : "&")."modes=".$aParams['modes'];
$i++;
}
if(isset($aParams['start']) && !is_null($aParams['start'])){
$sUrl .= ($i == 0 ? "?" : "&")."start=".$aParams['start'];
$i++;
}
if(isset($aParams['count']) && !is_null($aParams['count'])){
$sUrl .= ($i == 0 ? "?" : "&")."count=".$aParams['count'];
$i++;
}
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getPostGameCarnage
*
* Return datas of given match
*
* @param $sMatchId: id of the match wanted
* @param $sMatchType: type of the wanted match (arena, campaign, custom or warzone)
*
* @return $oJson: json object containing match datas
*/
public function getPostGameCarnage($sMatchId, $sMatchType){
$sUrl = self::BASE_URL."stats/".$this->sTitle."/".$sMatchType."/matches/".$sMatchId;
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
/**
* @name getServiceRecords
*
* Return datas of given match
*
* @param $sMatchType: type of the wanted match (arena, campaign, custom or warzone)
*
* @return $oJson: json object containing match datas
*/
public function getServiceRecords($sMatchType, $sSeasonId = null){
$sUrl = self::BASE_URL."stats/".$this->sTitle."/servicerecords/".$sMatchType;
foreach($this->aPlayerNames as $id => $val){
$sUrl .= ($id == 0 ? "?players=" : ",").$val;
}
if(!is_null($sSeasonId)){
$sUrl.= "?seasonId=".$sSeasonId;
}
$response = $this->callAPI($sUrl);
return $this->decodeJson($response['body']);
}
###
}