forked from JDMcKinstry/lolAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlolAPI.php
471 lines (422 loc) · 18.7 KB
/
lolAPI.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
<?php
/** CLASS lolAPI
* @created 01/01/2014
* @author JD McKinstry <[email protected]>
* @uses CURL
* @copyright Copyright (c) 2014, J.D. McKinstry
* @license https://github.com/JDMcKinstry/lolAPI/blob/master/LICENSE
* @modified 01/30/2014
* @modifiedby JD McKinstry
* @contributions
* - awaiting contributions
*/
class lolAPI {
/* Init Construction */
const API_KEY = '';
private $apiKey;
private $rateLimits = array(
'opm' => 10, // offset per minute
'rpm' => 50, // request per minute
'rps' => 1 // request per second
);
function __construct($apiKey=self::API_KEY) {
$this->apiKey = $apiKey;
if (empty($this->apiKey)) return "Please provide an API Key.";
}
/* Constants */
const BASE_URL = 'https://euw.api.pvp.net';
const FAIL = 'FAIL';
const SUCCESS = 'SUCCESS';
/* Properties:Private */
private $apiUrls = array(
'champion' => array(
'url' => array(
'all' => '/api/lol/{region}/{version}/champion',
'by-id' => '/api/lol/{region}/{version}/champion/{championId}'
),
'ver' => '1.2'
),
'game' => array(
'url' => '/api/lol/{region}/{version}/game/by-summoner/{summonerID}/recent',
'ver' => '1.3'
),
'league' => array(
'url' => array(
'summary' => '/api/lol/{region}/{version}/league/by-summoner/{summonerID}',
'entry ' => '/api/lol/{region}/{version}/league/by-summoner/{summonerID}/entry',
'by-team' => '/api/lol/{region}/{version}/league/by-team/{teamIds}',
'by-team-entry' => '/api/lol/{region}/{version}/league/by-team/{teamIds}/entry',
'challenger' => '/api/lol/{region}/{v2.4}version}/league/challenger'
),
'ver' => '2.4'
),
'static-data' => array(
'url' => array (
'champions' => '/api/lol/static-data/{region}/{version}/champion',
'champion' => '/api/lol/static-data/{region}/{version}/champion/{id}',
'items' => '/api/lol/static-data/{region}/{version}/item',
'item' => '/api/lol/static-data/{region}/{version}/item/{id}',
'masteries' => '/api/lol/static-data/{region}/{version}/mastery',
'mastery' => '/api/lol/static-data/{region}/{version}/mastery/{id}',
'realm' => '/api/lol/static-data/{region}/{version}/realm',
'runes' => '/api/lol/static-data/{region}/{version}/rune/{id}',
'rune' => '/api/lol/static-data/{region}/{version}/rune/{id}',
'spells' => '/api/lol/static-data/{region}/{version}/summoner-spell',
'spell' => '/api/lol/static-data/{region}/{version}/summoner-spell/{id}',
'summoner-spell' => '/api/lol/static-data/{region}/{version}/summoner-spell/{id}',
'versions' => 'api/lol/static-data/{region}/{version}/versions'
),
'ver' => '1.2'
),
'stats' => array(
'url' => array (
'summary' => '/api/lol/{region}/{version}/stats/by-summoner/{summonerID}/summary',
'ranked' => '/api/lol/{region}/{version}/stats/by-summoner/{summonerID}/ranked'
),
'ver' => '1.3'
),
'summoner' => array(
'url' => array (
'by-id' => '/api/lol/{region}/{version}/summoner/{summonerID}',
'by-name' => '/api/lol/{region}/{version}/summoner/by-name/{summonerName}',
'name' => '/api/lol/{region}/{version}/summoner/{summonerID}/name',
'masteries' => '/api/lol/{region}/{version}/summoner/{summonerID}/masteries',
'runes' => '/api/lol/{region}/{version}/summoner/{summonerID}/runes',
),
'ver' => '1.4'
),
'team' => array(
'url' => array(
'by-summoner' => '/api/lol/{region}/{version}/team/by-summoner/{summonerID}',
'by-id' => '/api/lol/{region}/{version}/team/{teamIds}'
),
'ver' => '2.3'
)
);
private $errorCodes = array(
200 => 'SUCCESS',
400 => 'Bad request',
//401 => 'Missing api key',
401 => 'Unauthorized',
404 => 'Data not found',
500 => 'Internal server error',
503 => 'Internal server error'
);
private $gameModes = array(
'CLASSIC' => "Classic Summoner's Rift and Twisted Treeline games",
'ODIN' => 'Dominion/Crystal Scar games',
'ARAM' => 'ARAM games',
'TUTORIAL' => 'Tutorial games',
'ONEFORALL' => 'One for All games',
'FIRSTBLOOD' => 'Snowdown Showdown games'
);
private $gameTypes = array(
'base' => array(
'CUSTOM_GAME' => 'Custom games',
'TUTORIAL_GAME' => 'Tutorial games',
'MATCHED_GAME' => 'All other games',
),
'sub' => array(
'NONE' => 'Custom games',
'NORMAL' => "Summoner's Rift unranked games",
'NORMAL_3x3' => 'Twisted Treeline unranked games',
'ODIN_UNRANKED' => 'Dominion/Crystal Scar games',
'ARAM_UNRANKED_5x5' => 'ARAM / Howling Abyss games',
'BOT' => "Summoner's Rift and Crystal Scar games played against AI",
'BOT_3x3' => 'Twisted Treeline games played against AI',
'RANKED_SOLO_5x5' => "Summoner's Rift ranked solo queue games",
'RANKED_TEAM_3x3' => 'Twisted Treeline ranked team games',
'RANKED_TEAM_5x5' => "Summoner's Rift ranked team games",
'ONEFORALL_5x5' => 'One for All games',
'FIRSTBLOOD_1x1' => 'Snowdown Showdown 1x1 games',
'FIRSTBLOOD_2x2' => 'Snowdown Showdown 2x2 games'
)
);
private $maps = array(
1 => array( 'name' => "Summoner's Rift", 'notes' => 'Summer Variant' ),
2 => array( 'name' => "Summoner's Rift", 'notes' => 'Autumn Variant' ),
3 => array( 'name' => 'The Proving Grounds', 'notes' => 'Tutorial Map' ),
4 => array( 'name' => 'Twisted Treeline', 'notes' => 'Original Version' ),
8 => array( 'name' => 'The Crystal Scar', 'notes' => 'Dominion Map' ),
10 => array( 'name' => 'Twisted Treeline', 'notes' => 'Current Version' ),
12 => array( 'name' => 'Howling Abyss', 'notes' => 'ARAM Map' )
);
private $runeSlots = array(
'glyphs' => array( 19, 20, 21, 22, 23, 24, 25, 26, 27 ),
'marks' => array( 1, 2, 3, 4, 5, 6, 7, 8, 9 ),
'quints' => array( 28, 29, 30 ),
'seals' => array( 10, 11, 12, 13, 14, 15, 16, 17, 18 )
);
private $regions = array( // BR, EUNE, EUW, KR, LAN, LAS, NA, OCE, RU, TR
'br' => 'Brazil',
'eune' => 'Eastern Europe',
'euw' => 'Western Europe',
'kr' => 'Korea',
'lan' => '',
'las' => '',
'na' => 'North America',
'oce' => '',
'ru' => '',
'tr' => 'Tournament'
);
/* Methods:Public:Utility */
/** callAPI(string, $url [, array $data]);
* @param STRING $url Must have URL to try to CURL
* @param ARRAY Data to add to query string for CURL call
* @return ARRAY Results from CURL call
*/
public function callAPI($url, $data=array()) {
$query = '?api_key=' . $this->apiKey;
if (!empty($data)) $query .= '&' . (is_array($data) ? http_build_query($data) : $data);
if (!empty($url)) {
$curlOpts = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 3,
CURLOPT_URL => $url.$query,
CURLOPT_VERBOSE => 1
);
$ch = curl_init($url);
curl_setopt_array($ch, $curlOpts);
$response = curl_exec($ch);
$result = $this->formatResult($ch, $response);
curl_close($ch);
return $this->result = $result;
}
return FALSE;
}
/** getRegions([bool $fullNames]);
* @param BOOLEAN $fullNames If set to TRUE, will return an array of Server Names instead of abbriviations
* @return ARRAY List of server abbriviations
*/
public function getRegions($fullNames=FALSE) { return $fullNames ? $this->regions : array_keys($this->regions); }
/** getRegions([string $value, bool $strict]);
* @note Does not have to be exact. Only requires $value to match Abbriviations by 80% or Names by 90%.
* @param STRING $value The name of the server you want the abbriviation for |OR| the abbriviation you want the server name for
* @param BOOLEAN $strict If set to true, then $value must be an exact match in Server Name or Abbriviation
* @return STRING If your value is an abbriviation, will return Name, or vice versa.
* @return DEFAULT The current abbriviation for the North American Server
*/
public function getRegion($value=NULL, $strict=FALSE) {
if (!empty($value)) {
if (key_exists($value, $this->regions)) return $this->regions[$value];
if ($strict) {
$key = array_search($value, array_map('strtolower',$this->regions));
return is_string($key) ? $key : self::getRegion('North America');
}
else $value = strtolower($value);
$key = array_search($value, array_map('strtolower',$this->regions));
if (is_string($key)) return $key;
foreach($this->regions as $k => $v) {
similar_text($k, $value, $kPerc);
if ($kPerc >= 80) return $v;
similar_text(strtolower($v), $value, $vPerc);
if ($vPerc >= 90) return $k;
}
}
return self::getRegion('North America');
}
/** getUrl(string $key, string $id, [, string $subKey, string $region, string $version ]);
* @param STRING $key the method name you seek to use from the LoL REST API
* @param STRING $id may be a summoner's name or id, or an item id
* @param STRING $subKey if there is a sub-method, such as seen on: /api/lol/static-data/{region}/v1/mastery <-- "mastery" is the submethod
* @param STRING $region server to try to pull from
* @param STRING $version version of method to use, if not set, will use latest version set in url's property
* @return STRING Returns url for use with LoL REST API
*/
public function getUrl($key, $id, $subKey=NULL, $region=NULL, $version=NULL) {
if (key_exists($key, $this->apiUrls)) {
$url = $verifyUrl = $this->apiUrls[$key]['url'];
if (is_array($url) && is_string($subKey)) {
if (key_exists($subKey, $url)) $verifyUrl = $url = $url[$subKey];
else return FALSE;
}
if (is_null($version)) $version = $this->apiUrls[$key]['ver'];
if (!key_exists($region, $this->regions)) $region = self::getRegion($region);
if (is_string($url) && key_exists($region, $this->regions) && !is_null($version)) {
$url = str_replace('{version}', "v$version", str_replace('{region}', $region, $url));
if (!empty($id)) $url = preg_replace('/{(summonerID|summonerName|id)}/', $id, $url);
$url = preg_replace('[//]', '/', preg_replace('/{(.*?)}/', '', $url));
$verifyUrl = preg_replace('/{(.*?)}/', '(.*?)', $verifyUrl);
if (substr($verifyUrl, strlen($verifyUrl)-6) == '/(.*?)') $verifyUrl = substr($verifyUrl, 0, -6) . '[/\d\w\s]*';
if(substr($url, -1) == '/') $url = substr($url, 0, -1);
return $this->url = preg_match('['.$verifyUrl.']', $url) ? self::BASE_URL.$url : FALSE;
}
}
return FALSE;
}
/* Methods:Public:Basic */
/** champion(bool $freeToPlay, string $region, string $version)
* @return ARRAY Retrieve all champions
*/
public function champion($freeToPlay=FALSE, $region=NULL, $version=NULL) { return self::callAPI(self::getUrl('champion', NULL, NULL, $region, $version), array( 'freeToPlay' => $freeToPlay == TRUE ? 'true' : 'false' )); }
/** game(long $id, string $region, string $version)
* @return ARRAY Get recent games by summoner ID
*/
public function game($id, $region=NULL, $version=NULL) { return self::callAPI(self::getUrl('game', $id, NULL, $region, $version)); }
/** champion(long $id, string $region, string $version)
* @return ARRAY Retrieves leagues data for summoner, including leagues for all of summoner's teams
*/
public function league($id, $region=NULL, $version=NULL) { return self::callAPI(self::getUrl('league', $id, NULL, $region, $version)); }
/** staticData(long $id, string $region, string $version) [DOES NOT COUNT TOWARD RATE LIMIT]
* @return ARRAY "champion" Retrieves champion list.
* @return ARRAY "champion/{id}" Retrieves a champion by its id.
* @return ARRAY "item" Retrieves item list.
* @return ARRAY "item/{id}" Retrieves item by its unique id.
* @return ARRAY "mastery" Retrieves mastery list.
* @return ARRAY "mastery/{id}" Retrieves mastery item by its unique id.
* @return ARRAY "realm" Retrieve realm data.
* @return ARRAY "rune" Retrieves rune list.
* @return ARRAY "rune/{id}" Retrieves rune by its unique id.
* @return ARRAY "summoner-spell" Retrieves summoner spell list.
* @return ARRAY "summoner-spell/{id}" Retrieves summoner spell by its unique id.
*/
public function staticData($subKey, $id=NULL, $data=NULL, $region=NULL, $version=NULL) { return self::callAPI(self::getUrl('static-data', $id, $subKey, $region, $version), $data); }
/** stats(long $id, string $region, string $version)
* @return ARRAY "summary" Get player stats summaries by summoner ID. One summary is returned per queue type.
* @return ARRAY "ranked" Get ranked stats by summoner ID. Includes statistics for Twisted Treeline and Summoner's Rift.
*/
public function stats($id, $subKey, $data=NULL, $region=NULL, $version=NULL) { return self::callAPI(self::getUrl('stats', $id, $subKey, $region, $version), $data); }
/** summoner(long $id, string $region, string $version)
* @return ARRAY "by-id" Get summoner objects mapped by summoner ID for a given list of summoner IDs
* @return ARRAY "by-name" Get summoner objects mapped by standardized summoner name for a given list of summoner names
* @return ARRAY "name" Get summoner names mapped by summoner ID for a given list of summoner IDs
* @return ARRAY "masteries" Get mastery pages mapped by summoner ID for a given list of summoner IDs
* @return ARRAY "runes" Get rune pages mapped by summoner ID for a given list of summoner IDs
*/
public function summoner($id, $subKey, $data=NULL, $region=NULL, $version=NULL) { return self::callAPI(self::getUrl('summoner', $id, $subKey, $region, $version), $data); }
/** champion(long $id, string $region, string $version)
* @return ARRAY Retrieves teams for given summoner ID
*/
public function team($id, $region=NULL, $version=NULL) { return self::callAPI(self::getUrl('team', $id, NULL, $region, $version)); }
/* Methods:Public:DataDragonDirect [DOES NOT COUNT TOWARD RATE LIMIT] */
const DATADRAGON_URL = "http://ddragon.leagueoflegends.com/realms/{region}.json";
public function callDataDragon($url, $region=NULL) {
if (empty($region)) $region = self::getRegion();
if (!empty($url)) {
$curlOpts = array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_TIMEOUT => 3,
CURLOPT_URL => $url,
CURLOPT_VERBOSE => 1
);
$ch = curl_init($url);
curl_setopt_array($ch, $curlOpts);
$response = curl_exec($ch);
$result = $this->formatResult($ch, $response);
curl_close($ch);
return $this->result = $result;
}
return FALSE;
}
public function getUrlDataDragon($method, $region=NULL) {
if (is_null($region)) $region = self::getRegion($region);
if (!key_exists($region, $this->regions)) $region = self::getRegion($region);
echo($region);
if (is_string($method) && key_exists($region, $this->regions)) {
$nfo = self::ddGetNfo($region, TRUE);
if (key_exists('status', $nfo))
if ($nfo['status'] == "SUCCESS" && key_exists('response', $nfo))
if (!empty($nfo['response']['cdn']) && !empty($nfo['response']['v']) && !empty($nfo['response']['l']))
return sprintf('%s/%s/data/%s/%s', $nfo['response']['cdn'], $nfo['response']['v'], $nfo['response']['l'], $method);
}
return FALSE;
}
/** ddGetNfo();
*/
public function ddGetNfo($region=NULL, $justResponse=FALSE) {
if (empty($region)) $region = self::getRegion();
$url = str_replace('{region}', $region, self::DATADRAGON_URL);
return self::callDataDragon($url);
}
/** ddChampionJSON();
*/
public function ddChampionJSON($region=NULL) { return self::callDataDragon(self::getUrlDataDragon('champion.json', $region), $region); }
/** ddItemJSON();
*/
public function ddItemJSON($region=NULL) { return self::callDataDragon(self::getUrlDataDragon('item.json', $region), $region); }
/** ddMasteryJSON();
*/
public function ddMasteryJSON($region=NULL) { return self::callDataDragon(self::getUrlDataDragon('mastery.json', $region), $region); }
/** ddRuneJSON();
*/
public function ddRuneJSON($region=NULL) { return self::callDataDragon(self::getUrlDataDragon('rune.json', $region), $region); }
/** ddSummonerJSON();
*/
public function ddSummonerJSON($region=NULL) { return self::callDataDragon(self::getUrlDataDragon('summoner.json', $region), $region); }
/* Methods:Private:Utility */
private function formatResult($ch, $response) {
$curlNfo = curl_getinfo($ch);
$errCode = curl_errno($ch);
if ($errCode) {
$errMsg = curl_error($ch);
$result = array(
'status' => self::FAIL,
'code' => $errCode,
'curlNfo' => $curlNfo,
'msg' => $errMsg
);
}
else {
$objResponse = json_decode($response);
if (empty($objResponse)) {
$errCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$errMsg = $this->errorCodes[$errCode];
$result = array(
'status' => self::FAIL,
'code' => $errCode,
'curlNfo' => $curlNfo,
'msg' => $errMsg
);
}
else {
$araResponse = $this->objToArr($objResponse);
$result = array(
'status' => self::SUCCESS,
'curlNfo' => $curlNfo,
'response' => $araResponse
);
if (isset($araResponse['status'])) {
if (isset($araResponse['status']['status_code'])) {
$result = array(
'status' => self::FAIL,
'code' => $araResponse['status']['status_code'],
'curlNfo' => $curlNfo,
'msg' => $araResponse['status']['message']
);
}
}
}
}
return $result;
}
private function objToArr($obj) {
$ret = array();
foreach ($obj as $k => $v) {
if (is_array($v) || is_object($v)) $v = $this->objToArr($v);
$ret[$k] = $v;
}
return $ret;
}
}
/* NOTES */
/*
champion - *freeToPlay
game - **summonerID
league - **summonerID
static-data-champion - **champID, *champData[ 'all', 'image', 'skins', 'lore', 'blurb', 'allytips', 'enemytips', 'tags', 'partype', 'info', 'stats', 'spells', 'passive', 'recommended' ]
static-data-item - **itemID, *itemData[ 'all', 'description', 'colloq', 'into', 'image', 'gold', 'tags', 'stats' ]
static-data-mastery - **itemID, *locale[ 'en_US', 'es_ES' ], *version, *masteryData[ 'all', 'ranks', 'prereq', 'image' ]
static-data-realm -
static-data-rune - **runeID, *locale[ 'en_US', 'es_ES' ], *version, *runeData[ 'all', 'image', 'stats', 'tags', 'colloq', 'plaintext' ]
static-data-spell - **spellID, *locale[ 'en_US', 'es_ES' ], *version, *spellData[ 'all', 'key', 'image', 'tooltip', 'resource', 'maxrank', 'modes', 'costType', 'cost', 'costBurn', 'range', 'rangeBurn', 'effect', 'effectBurn', 'cooldown', 'cooldownBurn', 'vars' ]
stats-summary - **summonerID, season[ 'SEASON3', 'SEASON4' ]
stats-ranked - **summonerID, season[ 'SEASON3', 'SEASON4' ]
{Comma-separated list of summoner Names/IDs to retrieve. Maximum allowed at once is 40.}
summoner-by_id - **summonerID
summoner-by_name - **summonerName
summoner-masteries - **summonerID
summoner-runes - **summonerID
summoner-name - **summonerID
team - **summonerID
*/
?>