Skip to content

Instantly share code, notes, and snippets.

@hakre
Last active December 27, 2015 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hakre/b654d1b7366273335dfd to your computer and use it in GitHub Desktop.
Save hakre/b654d1b7366273335dfd to your computer and use it in GitHub Desktop.
<?php
/**
* Class EveApiError
*/
class EveApiError extends Exception
{
}
/**
* Class EveApiXml
*/
class EveApiXml extends SimpleXMLElement
{
public function getError()
{
list($result) = $this->xpath('/*/error[1]') + [NULL];
if ($result) {
$result = new EveApiError((string)$result, (int)$result['code']);
}
return $result;
}
public function getErrorException()
{
if (!$error = $this->getError()) {
throw new BadMethodCallException('Only callable if there is an error.');
}
return $error;
}
}
/**
* Class EveApiResult
*/
class EveApiResult
{
/**
* @var EveApiXml
*/
private $xml;
public function __construct(EveApiXml $xml) {
$this->xml = $xml;
}
public function isError() {
return (bool) $this->xml->getError();
}
public function getErrorException() {
return $this->xml->getErrorException();
}
}
/**
* Class EveApiRequest
*/
class EveApiRequest
{
private $url;
private $loader;
/**
* @var array
*/
private $names;
/**
* @var array
*/
private $defaultArgs;
/**
* @var array
*/
private $overrideArgs;
function __construct($url, $loader, array $names, array $defaultArgs, array $overrideArgs)
{
$this->url = $url;
$this->loader = $loader;
$this->names = $names;
$this->defaultArgs = $defaultArgs;
$this->overrideArgs = $overrideArgs;
}
/**
* @internal param $args
* @throws RuntimeException
* @return EveApiResult;
*/
public function execute()
{
$positional = func_get_args();
$named = array_combine($this->names, $positional);
$args = $this->overrideArgs + $named + $this->defaultArgs;
$url = $this->url . '?' . http_build_str($args);
$loader = $this->loader;
$buffer = $loader($url);
if (FALSE === $buffer) {
throw new RuntimeException("Eveapi I/O Error");
}
$previous = libxml_use_internal_errors(TRUE);
$xml = simplexml_load_string($buffer, 'EveApiXml');
libxml_use_internal_errors($previous);
if (!$xml) {
throw new UnexpectedValueException("Eveapi XML Error");
}
echo $xml->asXML();
return new EveApiResult($xml);
}
public function __invoke() {
return call_user_func_array([$this, 'execute'], func_get_args());
}
}
/**
* Class EveApi
*/
class EveApi
{
const THROW_EXCEPTIONS_ON_ERROR = 1;
private $flags = self::THROW_EXCEPTIONS_ON_ERROR;
private $apiArgs;
private $prefix;
private $methods;
public function __construct(array $apiArgs, $prefix = 'https://api.eveonline.com/')
{
$this->apiArgs = $apiArgs;
$this->prefix = $prefix;
}
/**
* @param $path
* @param $names
* @param array $defaultArgs
* @internal param array $args
* @return EveApiRequest
*/
public function prepare($path, $names, array $defaultArgs = array())
{
$loader = function($url) {
$context = stream_context_create(['http' => ['ignore_errors' => true]]);
return file_get_contents($url, NULL, $context);
};
return new EveApiRequest($this->prefix . $path, $loader, $names, $defaultArgs, $this->apiArgs);
}
public function define($name, $path, array $names = array(), array $defaultArgs = array())
{
return $this->methods[$name] = [$path, $names, $defaultArgs];
}
function __call($name, $arguments)
{
if (!isset($this->methods[$name])) {
throw new BadMethodCallException(sprintf('Undefined method "%s"', $name));
}
$method = call_user_func_array([$this, 'prepare'], $this->methods[$name]);
/* @var $result EveApiResult */
$result = $method->execute($arguments);
if (($this->flags & self::THROW_EXCEPTIONS_ON_ERROR) and $result->isError()) {
throw $result->getErrorException();
}
return $result;
}
}
$legacyKey = [
'userID' => '8166034',
'apiKey' => 'B174C8B7B4364048B8A44B8C494904059D50B942BB4748FD907FF1DBF3F18282',
];
$api = new EveApi($legacyKey);
$api->define('getAccountCharacters', 'account/Characters.xml.aspx', ['characterID']);
try {
$characters = $api->getAccountCharacters($characterID = '91242713');
} catch(Exception $exception) {
printf("Exception: %s; Code: %s; Message: %s\n", get_class($exception), $exception->getCode(), $exception->getMessage());
throw $exception;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment