Created
September 24, 2016 10:45
-
-
Save nimmneun/3ec3b14d7fb3ec822b3fecfaf49596c8 to your computer and use it in GitHub Desktop.
Sloppy way to fetch characters of a player bungie api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class BungieClient ... really quick and dirty way. | |
*/ | |
class BungieClient | |
{ | |
/** | |
* @var array | |
*/ | |
private $classMapping = array( | |
'3159615086' => 'Glimmer', | |
'1415355184' => 'Crucible Marks', | |
'1415355173' => 'Vanguard Marks', | |
'898834093' => 'Exo', | |
'3887404748' => 'Human', | |
'2803282938' => 'Awoken', | |
'3111576190' => 'Male', | |
'2204441813' => 'Female', | |
'671679327' => 'Hunter', | |
'3655393761' => 'Titan', | |
'2271682572' => 'Warlock', | |
'3871980777' => 'New Monarchy', | |
'529303302' => 'Cryptarch', | |
'2161005788' => 'Iron Banner', | |
'452808717' => 'Queen', | |
'3233510749' => 'Vanguard', | |
'1357277120' => 'Crucible', | |
'2778795080' => 'Dead Orbit', | |
'1424722124' => 'Future War Cult', | |
'2033897742' => 'Weekly Vanguard Marks', | |
'2033897755' => 'Weekly Crucible Marks', | |
); | |
/** | |
* @var string | |
*/ | |
private $apiKey; | |
/** | |
* @param string $apiKey | |
*/ | |
public function __construct($apiKey) | |
{ | |
$this->apiKey = $apiKey; | |
} | |
/** | |
* Request character descriptions for given player and platform. | |
* | |
* @param string $playerName | |
* @param int $platformId | |
* @return array | |
*/ | |
public function fetchCharacterDescriptions($playerName, $platformId) | |
{ | |
$characterDescriptions = array(); | |
foreach ($this->fetchCharacters($playerName, $platformId) as $character) { | |
$char = new stdClass; | |
$char->class = $this->getClassNameByHash($character->characterBase->classHash); | |
$char->emblem = $character->emblemPath; | |
$char->backgroundpath = $character->backgroundPath; | |
$char->character_id = $character->characterBase->characterId; | |
$char->characterlevel = $character->characterLevel; | |
$char->light = $character->characterBase->stats->STAT_LIGHT->value; | |
$char->playerName = $playerName; | |
$characterDescriptions[] = $char; | |
} | |
return $characterDescriptions; | |
} | |
/** | |
* Return player URL based on $platformId and $playerName. | |
* | |
* @param int $platformId | |
* @param string $playerName | |
* @return string | |
*/ | |
private function getPlayerUrl($platformId, $playerName) { | |
return sprintf('https://bungie.net/Platform/Destiny/SearchDestinyPlayer/%s/%s', | |
$platformId, $playerName); | |
} | |
/** | |
* Return character URL bases on membershipType and Id. | |
* | |
* @param int $membershipType | |
* @param int $membershipId | |
* @return string | |
*/ | |
private function getCharacterUrl($membershipType, $membershipId) { | |
return sprintf('http://bungie.net/Platform/Destiny/%s/Account/%s?ignorecase=true', | |
$membershipType, $membershipId); | |
} | |
/** | |
* Stringcast for 32bit php versions. | |
* | |
* @param string $classHash | |
* @return string|null | |
*/ | |
private function getClassNameByHash($classHash) | |
{ | |
return isset($this->classMapping[(string)$classHash]) | |
? $this->classMapping[(string)$classHash] | |
: null; | |
} | |
/** | |
* Perform actual api call. | |
* | |
* @param string $url | |
* @return stdClass|null | |
*/ | |
private function callBungieApi($url) | |
{ | |
$ch = curl_init($url); | |
curl_setopt_array($ch, array( | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_FOLLOWLOCATION => 1, | |
CURLOPT_SSL_VERIFYHOST => 0, | |
CURLOPT_SSL_VERIFYPEER => 0, | |
CURLOPT_HTTPHEADER => array("X-API-Key: {$this->apiKey}"), | |
) | |
); | |
return json_decode(curl_exec($ch)); | |
} | |
/** | |
* Fetch data for given player. | |
* | |
* @param string $playerName | |
* @param int $platformId | |
* @return stdClass|null | |
*/ | |
private function fetchPlayer($playerName, $platformId) | |
{ | |
$response = $this->callBungieApi($this->getPlayerUrl($platformId, $playerName)); | |
if (isset($response->Response[0]->membershipId)) { | |
return $response->Response[0]; | |
} | |
return null; | |
} | |
/** | |
* Retrieve all characters for the given player and platform. | |
* | |
* @param string $playerName | |
* @param int $platformId | |
* @return array | |
*/ | |
private function fetchCharacters($playerName, $platformId = 2) | |
{ | |
if ($playerResponse = $this->fetchPlayer($playerName, $platformId)) { | |
$characterResponse = $this->callBungieApi( | |
$this->getCharacterUrl($playerResponse->membershipType, $playerResponse->membershipId) | |
); | |
if ($characterResponse) { | |
return $characterResponse->Response->data->characters; | |
} | |
} | |
return array(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment