This repository has been archived on 2026-03-25. You can view files and clone it, but cannot push or open issues or pull requests.
league-search/league.php
2015-06-30 01:06:38 -07:00

407 lines
9.3 KiB
PHP
Executable file

<?php
/**
*
* League Of Legends API
*
* League Of Legends API class
*
*
* @author Juyoung Lee
* @since 02.28.2015
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @version 1.0
*/
class LeagueOfLegends {
/**
* The API Base URL
*/
const API_URL = 'https://na.api.pvp.net/api/lol/';
/**
* The API Static Data Base URL
*/
const API_STATIC_DATA_URL = 'https://global.api.pvp.net/api/lol/static-data/na/v1.2/';
/**
* The API Image Base URL
*/
const API_IMAGE_BASE_URL = 'http://ddragon.leagueoflegends.com/cdn/5.3.3/img/';
/**
* The League of Legends API Key
*
* @var string
*/
private $_apiKey = '66ea8d47-903a-4d41-ad8a-93ac89964ceb';
/**
* Default constructor
*
* @return void
*/
public function __construct() { }
/**
* Get the API key
*
* @return string
*/
private function getApiKey() {
return $_apiKey;
}
/**
* Set summoner objects
* <trim and lowercase included>
*
* @param string $summonerNames Summoner name
* @return mixed
*/
private function setSummoner($summonerNames) {
$summonerNames = preg_replace('/\s+/', '', $summonerNames);
$summonerNames = strtolower($summonerNames);
$json = file_get_contents(self::API_URL.'na/v1.4/summoner/by-name/'.$summonerNames .'?api_key='.$this->_apiKey, true);
return json_decode($json)->$summonerNames;
}
/**
* Get a summoner object
* <Possible objects: id, name, profileIconId, revisionDate, summonerLevel>
*
* @param string $summonerNames Summoner name
* @param string $obj An object to call
* @return mixed
*/
public function getSummonerObject($summonerNames, $obj) {
$json = file_get_contents(self::API_URL.'na/v1.4/summoner/by-name/'.$summonerNames .'?api_key='.$this->_apiKey, true);
return json_decode($json)->$summonerNames->$obj;
}
/**
* Get summoner id by summoner name
*
* @param string $p Summoner name
* @return mixed
*/
public function getSummonerId($p) {
return $this->setSummoner($p)->id;
}
/**
* Get summoner name registered by summoner name
*
* @param string $p Summoner name
* @return mixed
*/
public function getSummonerName($p) {
return $this->setSummoner($p)->name;
}
/**
* Get ID of the summoner icon by summoner name
*
* @param string $p Summoner name
* @return mixed
*/
public function getSummonerIconId($p) {
return $this->setSummoner($p)->profileIconId;
}
/**
* Get the image link of the summoner icon by profile icon id
* <Call this function inside src of img tag>
*
* @param string $p Profile Icon ID
* @return mixed
*/
public function getSummonerIconImageLink($p) {
return self::API_IMAGE_BASE_URL.'profileicon/'.$p.'.png';
}
/**
* Get Date in epho milliseconds summoner was last modified
* by summoner name
*
* @param string $p Summoner name
* @return mixed
*/
public function getSummonerRevisionDate($p) {
return $this->setSummoner($p)->revisionDate;
}
/**
* Get Date in epho milliseconds summoner was last modified
* by summoner name
*
* @param string $p Summoner name
* @return string
*/
public function getSummonerRevisionDateFormat($p) {
$summonerRevisionDate = $this->setSummoner($p)->revisionDate;
$epoch = microtime(true);
$epochDiffSeconds = $epoch - ($summonerRevisionDate / 1000);
$epochMinutes = floor( ($epochDiffSeconds / 60) % 60 );
$epochHours = floor( ( $epochDiffSeconds / 3600 ) % 24 );
$epochDays = floor( $epochHours / 24 );
$epochString = ' active ';
if( $epochDays > (365 * 2) )
{
$epochString .= 'over '. floor( ($epochDays / 365) % 365 ).' years ';
}
else if($epochDays > 365)
{
$epochString .= 'over a year ';
}
else if($epochDays > 60)
{
$epochString .= 'over '. floor( ($epochDays / 30) % 30 ).' months ';
}
else if($epochDays > 30)
{
$epochString .= 'over a month ';
}
else if($epochDays > 1)
{
$epochString .= $epochDays . ' days ';
}
else if($epochDays == 1)
{
$epochString .= ' a day ';
}
if($epochDays < 2 && $epochHours > 1)
{
$epochString .= $epochHours . ' hours ';
}
else if($epochDays < 2 && $epochHours == 1)
{
$epochString .= ' an hour ';
}
if($epochDays == 0 && $epochMinutes > 1)
{
$epochString .= $epochMinutes . ' minutes ';
}
else if($epochDays == 0 && $epochMinutes == 1)
{
$epochString .= ' one minute ';
}
$epochString .= 'ago';
if( $epochDiffSeconds < (60 * 5) )
{
$epochString = 'active now';
}
return $epochString;
}
/**
* Get summoner level
*
* @param string $p Summoner name
* @return mixed
*/
public function getSummonerLevel($p) {
return $this->setSummoner($p)->summonerLevel;
}
/**
* Get summoner tier
*
* @param string $p Summoner ID
* @return string
*/
public function getSummonerTier($p) {
$json = file_get_contents(self::API_URL.'na/v2.5/league/by-summoner/'. $p .'/entry?api_key='.$this->_apiKey);
$link = json_decode($json) -> $p;
$summonerTier = $link[0]->tier;
$summonerTier = ucfirst(strtolower($summonerTier));
$summonerDivision = $link[0]->entries[0]->division;
return $summonerTier;
}
/**
* Get summoner tier
*
* @param string $p Summoner ID
* @return mixed
*/
public function getSummonerDivision($p) {
$json = file_get_contents(self::API_URL.'na/v2.5/league/by-summoner/'. $p .'/entry?api_key='.$this->_apiKey);
$link = json_decode($json) -> $p;
return $link[0]->entries[0]->division;
}
/**
* Retrieves a champion by its id
*
* @param string $championId Champion ID
* @return mixed
*/
private function setChampionData($championId) {
$json = file_get_contents(self::API_STATIC_DATA_URL.'champion/'.$championId .'?api_key='.$this->_apiKey, true);
return json_decode($json);
}
/**
* Get champion title by champion id
*
* @param string $p Champion ID
* @return mixed
*/
public function getChampionTitle($p) {
return $this->setChampionData($p)->title;
}
/**
* Get champion name by champion id
*
* @param string $p Champion ID
* @return mixed
*/
public function getChampionName($p) {
return $this->setChampionData($p)->name;
}
/**
* Get champion key by champion id
*
* @param string $p Champion ID
* @return mixed
*/
public function getChampionKey($p) {
return $this->setChampionData($p)->key;
}
/**
* Get the champion link of the item by champion id
* <Call this function inside src of img tag>
*
* @param string $p Champion ID
* @return mixed
*/
public function getChampionImageLink($p) {
return self::API_IMAGE_BASE_URL.'champion/'.$this->getChampionKey($p).'.png';
}
/**
* Set item data objects by item id
*
* @param string $ItemId Item ID
* @return mixed
*/
private function setItemData($ItemId) {
$json = file_get_contents(self::API_STATIC_DATA_URL.'item/'.$ItemId .'?api_key='.$this->_apiKey, true);
return json_decode($json);
}
/**
* Get item plain text by item id
*
* @param string $p Item ID
* @return mixed
*/
public function getItemPlainText($p) {
return $this->setItemData($p)->plaintext;
}
/**
* Get item description by item id
*
* @param string $p Item ID
* @return mixed
*/
public function getItemDescription($p) {
$val = $this->setItemData($p)->description;
$val = str_replace("<br>", '
', $val);
return strip_tags($val);
}
/**
* Get item name and its description by item id
*
* @param string $p Item ID
* @return mixed
*/
public function getItemNameWithDescription($p) {
$val = $this->setItemData($p)->description;
$val = str_replace("<br>", '
', $val);
return $this->setItemData($p)->name . '
' . strip_tags($val);
}
/**
* Get item name by item id
*
* @param string $p Item ID
* @return mixed
*/
public function getItemName($p) {
return $this->setItemData($p)->name;
}
/**
* Get the image link of the item by item id
* <Call this function inside src of img tag>
*
* @param string $p Item ID
* @return mixed
*/
public function getItemImageLink($p) {
return self::API_IMAGE_BASE_URL.'item/'.$p.'.png';
}
/**
* Retrieves summoner spell by its unique id
*
* @param string $SpellId Spell ID
* @return mixed
*/
private function setSummonerSpell($spellId) {
$json = file_get_contents(self::API_STATIC_DATA_URL.'summoner-spell/'.$spellId .'?api_key='.$this->_apiKey, true);
return json_decode($json);
}
/**
* Get spell image link
*
* @param string $p Spell ID
* @return mixed
*/
public function getSpellImageLink($p) {
return self::API_IMAGE_BASE_URL.'spell/'.$this->setSummonerSpell($p)->key.'.png';
}
/**
* Get spell name
*
* @param string $p Spell ID
* @return mixed
*/
public function getSpellName($p) {
return $this->setSummonerSpell($p)->name;
}
/**
* Get spell description
*
* @param string $p Spell ID
* @return mixed
*/
public function getSpellDescription($p) {
return $this->setSummonerSpell($p)->description;
}
}
?>