Skip to content
This repository has been archived by the owner on Apr 4, 2021. It is now read-only.

Commit

Permalink
Added experimental support for the new api key system (keyID/vCode). …
Browse files Browse the repository at this point in the history
…You can switch to the new customKeys via PhealConfig. Check readme for details. As soon as the old api system is deprecated/offline we should cleanup the the files and variables userid=keyID, etc.
  • Loading branch information
dhoffend committed Aug 27, 2011
1 parent c0223be commit 48335b5
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 22 deletions.
23 changes: 12 additions & 11 deletions Pheal.php
Expand Up @@ -66,8 +66,8 @@ class Pheal

/**
* creates new Pheal API object
* @param int $userid the EVE userid
* @param string $key the EVE apikey
* @param int $userid the EVE userid/keyID
* @param string $key the EVE apikey/vCode
* @param string $scope scope to use, defaults to account. scope can be changed during usage by modifycation of public attribute "scope"
*/
public function __construct($userid=null, $key=null, $scope="account")
Expand Down Expand Up @@ -113,16 +113,16 @@ public function __get($name)
* @throws PhealException|PhealAPIException|PhealHTTPException
* @param string $scope api scope (examples: eve, map, server, ...)
* @param string $name api method (examples: ServerStatus, Kills, Sovereignty, ...)
* @param array $opts additional arguments (example: characterID => 12345, ...), should not contain apikey/userid
* @param array $opts additional arguments (example: characterID => 12345, ...), should not contain apikey/userid/keyid/vcode
* @return PhealResult
*/
private function request_xml($scope, $name, array $opts = array())
{
$opts = array_merge(PhealConfig::getInstance()->additional_request_parameters, $opts);

// apikey/userid should be allowed in arguments and removed to avoid wrong cached api calls
// apikey/userid/keyid|vcode should be allowed in arguments and removed to avoid wrong cached api calls
foreach($opts AS $k => $v) {
if(strtolower($k) == "userid" || strtolower($k) == "apikey")
if(in_array(strtolower($k), array('userid','apikey','keyid','vcode')))
unset($opts[$k]);
}

Expand All @@ -131,12 +131,13 @@ private function request_xml($scope, $name, array $opts = array())
{
// build url
$url = PhealConfig::getInstance()->api_base . $scope . '/' . $name . ".xml.aspx";
$use_customkey = (bool)PhealConfig::getInstance()->api_customkeys;

try {
// prepare http arguments (to not modify original argument list for cache saving)
$http_opts = $opts;
if($this->userid) $http_opts['userID'] = $this->userid;
if($this->key) $http_opts['apiKey'] = $this->key;
if($this->userid) $http_opts[($use_customkey?'keyID':'userid')] = $this->userid;
if($this->key) $http_opts[($use_customkey?'vCode':'apikey')] = $this->key;

// start measure the response time
PhealConfig::getInstance()->log->start();
Expand All @@ -160,17 +161,17 @@ private function request_xml($scope, $name, array $opts = array())
// other request errors
} catch(Exception $e) {
// log + throw error
PhealConfig::getInstance()->log->errorLog($scope,$name,$opts,$e->getCode() . ': ' . $e->getMessage());
throw new PhealException('API Date could not be read / parsed, orginial exception: ' . $e->getMessage());
PhealConfig::getInstance()->log->errorLog($scope,$name,$http_opts,$e->getCode() . ': ' . $e->getMessage());
throw new PhealException('API Date could not be read / parsed, original exception: ' . $e->getMessage());
}
PhealConfig::getInstance()->cache->save($this->userid,$this->key,$scope,$name,$opts,$this->xml);

// archive+save only non-error api calls + logging
if(!$element->error) {
PhealConfig::getInstance()->log->log($scope,$name,$opts);
PhealConfig::getInstance()->log->log($scope,$name,$http_opts);
PhealConfig::getInstance()->archive->save($this->userid,$this->key,$scope,$name,$opts,$this->xml);
} else {
PhealConfig::getInstance()->log->errorLog($scope,$name,$opts,$element->error['code'] . ': ' . $element->error);
PhealConfig::getInstance()->log->errorLog($scope,$name,$http_opts,$element->error['code'] . ': ' . $element->error);
}
} else {
$element = new SimpleXMLElement($this->xml);
Expand Down
6 changes: 6 additions & 0 deletions PhealConfig.php
Expand Up @@ -52,6 +52,12 @@ class PhealConfig
*/
public $api_base = "http://api.eveonline.com/";

/**
* enable the new customize key system (use keyID instead of userID, etc)
* @var bool
*/
public $api_customkeys = false;

/**
* associative array with additional parameters that should be passed
* to the API on every request.
Expand Down
2 changes: 1 addition & 1 deletion PhealFileArchive.php
Expand Up @@ -81,7 +81,7 @@ protected function filename($userid, $apikey, $scope, $name, $args)
{
if(strlen($val) < 1)
unset($args[$key]);
elseif($key != 'userid' && $key != 'apikey')
elseif(!in_array(strtolower($key), array('userid','apikey','keyid','vcode')))
$argstr .= $key . $this->options['delimiter'] . $val . $this->options['delimiter'];
}
$argstr = substr($argstr, 0, -1);
Expand Down
2 changes: 1 addition & 1 deletion PhealFileCache.php
Expand Up @@ -81,7 +81,7 @@ protected function filename($userid, $apikey, $scope, $name, $args)
foreach($args as $key => $val) {
if(strlen($val) < 1)
unset($args[$key]);
elseif($key != 'userid' && $key != 'apikey')
elseif(!in_array(strtolower($key), array('userid','apikey','keyid','vcode')))
$argstr .= $key . $this->options['delimiter'] . $val . $this->options['delimiter'];
}
$argstr = substr($argstr, 0, -1);
Expand Down
6 changes: 3 additions & 3 deletions PhealFileLog.php
Expand Up @@ -121,9 +121,9 @@ protected function formatUrl($scope,$name,$opts)
$url = PhealConfig::getInstance()->api_base . $scope . '/' . $name . ".xml.aspx";

// truncacte apikey for log safety
if($this->options['truncate_apikey']) {
if(count($opts) && isset($opts['apikey']))
$opts['apikey'] = substr($opts['apikey'],0,16)."...";
if($this->options['truncate_apikey'] && count($opts)) {
if(isset($opts['apikey'])) $opts['apikey'] = substr($opts['apikey'],0,16)."...";
if(isset($opts['vCode'])) $opts['vCode'] = substr($opts['vCode'],0,16)."...";
}

// add post data
Expand Down
2 changes: 1 addition & 1 deletion PhealMemcache.php
Expand Up @@ -72,7 +72,7 @@ protected function getKey($userid, $apikey, $scope, $name, $args)
{
$key = "$userid|$apikey|$scope|$name";
foreach($args as $k=>$v) {
if($k != 'userid' && $k != 'apikey')
if(!in_array(strtolower($key), array('userid','apikey','keyid','vcode')))
$key .= "|$k|$v";
}
return "Pheal_" . md5($key);
Expand Down
28 changes: 23 additions & 5 deletions README.md
Expand Up @@ -105,7 +105,7 @@ which is the EVE APIs error code, and also contains the EVE API message as messa
try {
$pheal->Killlog(array("characterID" => 12345));
} catch(PhealException $e) {
echo 'error: ' . $e->code . ' meesage: ' . $e->getMessage();
echo 'error: ' . $e->code . ' message: ' . $e->getMessage();
}

### Archiving
Expand All @@ -125,7 +125,7 @@ archives. Otherwise you end up with million xml files in your filesystem.
try {
$pheal->Sovereignty();
} catch(PhealException $e) {
echo 'error: ' . $e->code . ' meesage: ' . $e->getMessage();
echo 'error: ' . $e->code . ' message: ' . $e->getMessage();
}

### Logging
Expand All @@ -143,7 +143,7 @@ One 'pheal_access.log' for successful calls and a 'pheal_error.log' for failed r
try {
$pheal->Sovereignty();
} catch(PhealException $e) {
echo 'error: ' . $e->code . ' meesage: ' . $e->getMessage();
echo 'error: ' . $e->code . ' message: ' . $e->getMessage();
}

### HTTP request options
Expand Down Expand Up @@ -185,15 +185,33 @@ man-in-the-middle attacks then.
PhealConfig::getInstance()->http_ssl_verifypeer = false;

### Helper Function
The method **toArray()** can be called on any leven of the api result. It's usefull
The method **toArray()** can be called on any level of the api result. It's useful
if you wanna convert an api result object into a json string or if you wanna use the
result array in your favorite template engine.

$pheal = new Pheal();
$result = $pheal->eveScope->FacWarStats();
$array = $result->toArray();
$json = json_encode($array);


### Customizable API Keys
Pheal has experimental support the new customizable api key system. To be able to use
a new custom key, you've to change a config option and force Pheal to use the https
url. After that just use Pheal like you did before just replace userID with your keyID
and apiKey with vCode in your Pheal object init.
Atm, the custom key support is still in testing (use https://apitest.eveonline.com instead).

require_once "Pheal/Pheal.php";
spl_autoload_register("Pheal::classload");
PhealConfig::getInstance()->api_base = 'https://api.eveonline.com/';
PhealConfig::getInstance()->api_customkeys = true;
$pheal = new Pheal($keyID, $vCode);
try {
$result = $pheal->accountScope->APIKeyInfo();
} catch(PhealException $e) {
echo 'error: ' . $e->code . ' message: ' . $e->getMessage();
}

## TODO
- more documentation
- more error handling
Expand Down

0 comments on commit 48335b5

Please sign in to comment.