forked from kevinohashi/php-riot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-riot-api.php
530 lines (409 loc) · 15.1 KB
/
php-riot-api.php
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
<?php
/*
PHP Riot API
Kevin Ohashi (http://kevinohashi.com)
http://github.com/kevinohashi/php-riot-api
The MIT License (MIT)
Copyright (c) 2013 Kevin Ohashi
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
class riotapi {
const API_URL_PLATFORM_3 = "https://{platform}.api.riotgames.com/lol/platform/v3/";
const API_URL_CHAMPION_MASTERY_3 = "https://{platform}.api.riotgames.com/lol/champion-mastery/v3/";
const API_URL_SPECTATOR_3 = 'https://{platform}.api.riotgames.com/lol/spectator/v3/';
const API_URL_STATIC_3 = 'https://{platform}.api.riotgames.com/lol/static-data/v3/';
const API_URL_MATCH_3 = 'https://{platform}.api.riotgames.com/lol/match/v3/';
const API_URL_LEAGUE_3 = 'https://{platform}.api.riotgames.com/lol/league/v3/';
const API_URL_SUMMONER_3 = 'https://{platform}.api.riotgames.com/lol/summoner/v3/';
const API_KEY = 'INSERT_API_KEY_HERE';
// Rate limit for 10 minutes
const LONG_LIMIT_INTERVAL = 600;
const RATE_LIMIT_LONG = 500;
// Rate limit for 10 seconds'
const SHORT_LIMIT_INTERVAL = 10;
const RATE_LIMIT_SHORT = 10;
// Cache variables
const CACHE_LIFETIME_MINUTES = 60;
private $cache;
private $PLATFORM;
//variable to retrieve last response code
private $responseCode;
private static $errorCodes = array(0 => 'NO_RESPONSE',
400 => 'BAD_REQUEST',
401 => 'UNAUTHORIZED',
404 => 'NOT_FOUND',
429 => 'RATE_LIMIT_EXCEEDED',
500 => 'SERVER_ERROR',
503 => 'UNAVAILABLE');
// Whether or not you want returned queries to be JSON or decoded JSON.
// honestly I think this should be a public variable initalized in the constructor, but the style before me seems definitely to use const's.
// Remove this commit if you want. - Ahubers
const DECODE_ENABLED = TRUE;
public function __construct($platform, CacheInterface $cache = null)
{
$this->PLATFORM = $platform;
$this->shortLimitQueue = new SplQueue();
$this->longLimitQueue = new SplQueue();
$this->cache = $cache;
}
//Returns all champion information.
//Set $free at true to get only free champions.
public function getChampion($free = false){
$call = 'champions';
if($free)
$call .= '?freeToPlay=true';
//add API URL to the call
$call = self::API_URL_PLATFORM_3 . $call;
return $this->request($call);
}
//Returns all free champion information.
public function getFreeChampions(){
$call = 'champions?freeToPlay=true';
//add API URL to the call
$call = self::API_URL_PLATFORM_3 . $call;
return $this->request($call);
}
//Returns all champions mastery for a player
public function getChampionMastery($id, $championId = false){
$call = self::API_URL_CHAMPION_MASTERY_3 . 'champion-masteries/by-summoner/' . $id ;
if($championId)
$call .= "/by-champion/" . $championId;
return $this->request($call);
}
//gets current game information for player
public function getCurrentGame($id){
$call = self::API_URL_SPECTATOR_3 . 'active-games/by-summoner/' . $id;
return $this->request($call);
}
//performs a static call. Not counted in rate limit.
//$call is what is asked (champion, item...)
//$id the id for a specific item, champion. Set at null to get all champions, items...
//$params is the string you get after the "?"
// getStatic("champions", 1, "locale=fr_FR&tags=image&tags=spells") will get you image data and spells data in French from champion whose ID is 1, here Annie.
public function getStatic($call, $id = null, $params = null) {
$call = self::API_URL_STATIC_3 . $call;
if( $id !=null)
$call.="/" . $id;
if( $params !=null)
$call.="?" . $params;
return $this->request($call, true);
}
//New to my knowledge. Returns match details.
//Now that timeline is a separated call, when includedTimeline is true, two calls are done at the same time.
//Data is then processed to match the old structure, with timeline data included in the match data
public function getMatch($matchId, $includeTimeline = true) {
$call = self::API_URL_MATCH_3 . 'matches/' . $matchId;
if(!$includeTimeline)
return $this->request($call);
else
$timelineCall = self::API_URL_MATCH_3 . 'timelines/by-match/' . $matchId;
$data = $this->requestMultiple(array(
"data"=>$call,
"timeline"=>$timelineCall
));
$data["data"]["timeline"] = $data["timeline"];
return $data["data"];
}
//Returns timeline of a match
public function getTimeline($matchId){
$call = self::API_URL_MATCH_3 . 'timelines/by-match/' . $matchId;
return $this->request($call);
}
//Returns a user's matchList given their account id.
public function getMatchList($accountId,$params=null) {
if($params==null){
$call = self::API_URL_MATCH_3 . 'matchlists/by-account/' . $accountId;
}else{
$call = self::API_URL_MATCH_3 . 'matchlists/by-account/' . $accountId .'?';
//You can pass params either as an array or as string
if(is_array($params))
foreach($params as $key=>$param){
//each param can also be an array, a list of champions, queues or seasons
//refer to API doc to get details about params
if(is_array($param))
foreach($param as $p)
$call .= $key . '=' . $p . '&';
else
$call .= $key . '=' . $param . '&';
}
else
$call .= $params . '&';
}
return $this->request($call);
}
//Returns a user's recent matchList given their account id.
public function getRecentMatchList($accountId) {
$call = self::API_URL_MATCH_3 . 'matchlists/by-account/' . $accountId . '/recent';
return $this->request($call);
}
//Returns the league of a given summoner.
public function getLeague($id){
$call = 'leagues/by-summoner/' . $id;
//add API URL to the call
$call = self::API_URL_LEAGUE_3 . $call;
return $this->request($call);
}
//Returns the league position of a given summoner.
//Similar to the old league /entry
public function getLeaguePosition($id){
$call = 'positions/by-summoner/' . $id;
//add API URL to the call
$call = self::API_URL_LEAGUE_3 . $call;
return $this->request($call);
}
//Returns the challenger ladder.
public function getChallenger($queue = "RANKED_SOLO_5x5") {
$call = 'challengerleagues/by-queue/' . $queue;
//add API URL to the call
$call = self::API_URL_LEAGUE_3 . $call;
return $this->request($call);
}
//Returns the master ladder.
public function getMaster($queue = "RANKED_SOLO_5x5") {
$call = 'masterleagues/by-queue/' . $queue;
//add API URL to the call
$call = self::API_URL_LEAGUE_3 . $call;
return $this->request($call);
}
//returns a summoner's id
public function getSummonerId($name) {
$name = strtolower($name);
$summoner = $this->getSummonerByName($name);
if (self::DECODE_ENABLED) {
return $summoner['id'];
}
else {
$summoner = json_decode($summoner, true);
return $summoner['id'];
}
}
//returns an account id
public function getSummonerAccountId($name) {
$name = strtolower($name);
$summoner = $this->getSummonerByName($name);
if (self::DECODE_ENABLED) {
return $summoner['accountId'];
}
else {
$summoner = json_decode($summoner, true);
return $summoner['accountId'];
}
}
//Returns summoner info given summoner id or account id.
public function getSummoner($id,$accountId = false){
$call = 'summoners/';
if ($accountId) {
$call .= 'by-account/';
}
$call .= $id;
//add API URL to the call
$call = self::API_URL_SUMMONER_3 . $call;
return $this->request($call);
}
//Gets a summoner's info given their name, instead of id.
public function getSummonerByName($name){
$call = 'summoners/by-name/' . rawurlencode($name);
//add API URL to the call
$call = self::API_URL_SUMMONER_3 . $call;
return $this->request($call);
}
//Gets a summoner's masteries.
public function getMasteries($id){
$call = 'masteries/by-summoner/' . $id;
//add API URL to the call
$call = self::API_URL_PLATFORM_3 . $call;
return $this->request($call);
}
//Gets a summoner's runes.
public function getRunes($id){
$call = 'runes/by-summoner/' . $id;
//add API URL to the call
$call = self::API_URL_PLATFORM_3 . $call;
return $this->request($call);
}
//Gets data of matches, given array of id.
public function getMatches($ids, $includeTimeline = true){
$calls=array();
foreach($ids as $matchId){
$call = self::API_URL_MATCH_3 . 'matches/' . $matchId;
$calls["match-".$matchId] = $call;
if($includeTimeline)
$calls["timeline-".$matchId] = self::API_URL_MATCH_3 . 'timelines/by-match/' . $matchId;
}
if(!$includeTimeline)
return $this->requestMultiple($calls);
$results = array();
$data = $this->requestMultiple($calls);
foreach($data as $k=>$d){
$e = explode("-", $k);
//Check if it's match data
if($e[0]=="match"){
//Check if the timeline exists
//Timeline is only stored by Riot for one year, too old games may not have it
if(isset($data["timeline-".$e[1]]["frames"]))
//add the matching timeline
$d["timeline"] = $data["timeline-".$e[1]];
array_push($results, $d);
}
}
return $results;
}
private function updateLimitQueue($queue, $interval, $call_limit){
while(!$queue->isEmpty()){
/* Three possibilities here.
1: There are timestamps outside the window of the interval,
which means that the requests associated with them were long
enough ago that they can be removed from the queue.
2: There have been more calls within the previous interval
of time than are allowed by the rate limit, in which case
the program blocks to ensure the rate limit isn't broken.
3: There are openings in window, more requests are allowed,
and the program continues.*/
$timeSinceOldest = time() - $queue->bottom();
// I recently learned that the "bottom" of the
// queue is the beginning of the queue. Go figure.
// Remove timestamps from the queue if they're older than
// the length of the interval
if($timeSinceOldest > $interval){
$queue->dequeue();
}
// Check to see whether the rate limit would be broken; if so,
// block for the appropriate amount of time
elseif($queue->count() >= $call_limit){
if($timeSinceOldest < $interval){ //order of ops matters
echo("sleeping for".($interval - $timeSinceOldest + 1)." seconds\n");
sleep($interval - $timeSinceOldest);
}
}
// Otherwise, pass through and let the program continue.
else {
break;
}
}
// Add current timestamp to back of queue; this represents
// the current request.
$queue->enqueue(time());
}
private function request($call, $static = false) {
//format the full URL
$url = $this->format_url($call);
//echo $url;
//caching
if($this->cache !== null && $this->cache->has($url)){
$result = $this->cache->get($url);
} else {
// Check rate-limiting queues if this is not a static call.
if (!$static) {
$this->updateLimitQueue($this->longLimitQueue, self::LONG_LIMIT_INTERVAL, self::RATE_LIMIT_LONG);
$this->updateLimitQueue($this->shortLimitQueue, self::SHORT_LIMIT_INTERVAL, self::RATE_LIMIT_SHORT);
}
//call the API and return the result
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-Riot-Token: '. self::API_KEY
));
$result = curl_exec($ch);
$this->responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($this->responseCode == 200) {
if($this->cache !== null){
$this->cache->put($url, $result, self::CACHE_LIFETIME_MINUTES * 60);
}
} else {
throw new Exception(self::$errorCodes[$this->responseCode]);
}
}
if (self::DECODE_ENABLED) {
$result = json_decode($result, true);
}
return $result;
}
private function requestMultiple($calls) {
$urls=array();
$results=array();
foreach($calls as $k=>$call){
$url = $this->format_url($call);
//Put cached data in resulsts and urls to call in urls
if($this->cache !== null && $this->cache->has($url)){
if (self::DECODE_ENABLED) {
$results[$k] = json_decode($this->cache->get($url), true);
}else{
$results[$k] = $this->cache->get($url);
}
} else {
$urls[$k] = $url;
}
}
$callResult=$this->multiple_threads_request($urls);
foreach($callResult as $k=>$result){
if($this->cache !== null){
$this->cache->put($urls[$k], $result, self::CACHE_LIFETIME_MINUTES * 60);
}
if (self::DECODE_ENABLED) {
$results[$k] = json_decode($result, true);
}else{
$results[$k] = $result;
}
}
return array_merge($results);
}
//creates a full URL you can query on the API
private function format_url($call){
return str_replace('{platform}', $this->PLATFORM, $call);
}
public function getLastResponseCode(){
return $this->responseCode;
}
public function debug($message) {
echo "<pre>";
print_r($message);
echo "</pre>";
}
public function setPlatform($platform) {
$this->PLATFORM = $platform;
}
private function multiple_threads_request($nodes){
$mh = curl_multi_init();
$curl_array = array();
foreach($nodes as $i => $url)
{
$curl_array[$i] = curl_init($url);
curl_setopt($curl_array[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_array[$i], CURLOPT_HTTPHEADER, array(
'X-Riot-Token: '. self::API_KEY
));
curl_multi_add_handle($mh, $curl_array[$i]);
}
$running = NULL;
do {
usleep(10000);
curl_multi_exec($mh,$running);
} while($running > 0);
$res = array();
foreach($nodes as $i => $url)
{
$res[$i] = curl_multi_getcontent($curl_array[$i]);
}
foreach($nodes as $i => $url){
curl_multi_remove_handle($mh, $curl_array[$i]);
}
curl_multi_close($mh);
return $res;
}
}