Skip to content

Instantly share code, notes, and snippets.

@marcaum54
Created March 20, 2013 22:52
Show Gist options
  • Save marcaum54/5209293 to your computer and use it in GitHub Desktop.
Save marcaum54/5209293 to your computer and use it in GitHub Desktop.
<?php
//72F82C4B0CBCC1704CDD2130E685E767
class Dota2_API
{
protected $_apiKey;
const HEROES_NAME_PREFIX = 'npc_dota_hero_';
const URL_GETHEROES = 'https://api.steampowered.com/IEconDOTA2_570/GetHeroes/v0001/';
const URL_IMAGE_HEROES = 'http://media.steampowered.com/apps/dota2/images/heroes/%NAME%_full.png';
public function __construct($key)
{
$this->_apiKey = $key;
}
public static function dump($a, $exit = true)
{
$style = array(
'color' => '#FFF',
'padding' => '25px',
'background-color' => '#333',
'boder' => '1px dashed #FFF000'
);
$style = implode(';', $style);
echo '<pre style="'. $style .'">';
print_r($a);
echo '</pre>';
if($exit)
exit;
}
public function getJson($url)
{
$obj = json_decode(file_get_contents($url));
if(!($obj instanceof stdClass))
throw new Exception_Dota2_API('not is Object');
return $obj;
}
public function getHeroes($id = null)
{
$url = self::URL_GETHEROES . '?key=' . $this->_apiKey;
$obj = $this->getJson($url);
if(!isset($obj->result))
throw new Exception_Dota2_API('->result not exist');
if(!isset($obj->result->heroes))
throw new Exception_Dota2_API('->result->heroes not exist');
$heroes = $obj->result->heroes;
if($id)
{
if(!isset($heroes[$id]))
throw new Exception_Dota2_API('not exist Heroes with id = "'. $id .'"');
$heroes = array($heroes[$id]);
}
foreach($heroes as &$hero)
{
$name = str_replace(self::HEROES_NAME_PREFIX, '', $hero->name);
$hero->image = str_replace('%NAME%', $name, self::URL_IMAGE_HEROES);
}
return $id ? $heroes[0] : $heroes;
}
}
class Exception_Dota2_API extends Exception
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment