Skip to content

Instantly share code, notes, and snippets.

@fuzzysteve
Created November 20, 2014 17:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuzzysteve/78c8952afc3d70d5f388 to your computer and use it in GitHub Desktop.
Save fuzzysteve/78c8952afc3d70d5f388 to your computer and use it in GitHub Desktop.
An initial draft of a handler for Crest. Market data only at the moment.
<?php
namespace FuzzyCrest;
use FuzzyCrest;
class CrestHandler
{
private $useragent="Fuzzwork Market agent 1.0";
private $expiry=0;
private $access_token='';
private $endpoints;
private $secret;
private $clientid;
private $refresh_token;
private $urlbase;
public function __construct($urlbase, $secret, $clientid, $refresh_token)
{
$this->expiry=0;
$this->access_token='';
$this->urlbase=$urlbase;
$this->endpoints=$this->getEndpoints();
$this->clientid=$clientid;
$this->secret=$secret;
$this->refresh_token=$refresh_token;
}
private function getAccessToken()
{
$header='Authorization: Basic '.base64_encode($this->clientid.':'.$this->secret);
$fields_string='';
$fields=array(
'grant_type' => 'refresh_token',
'refresh_token' => $this->refresh_token
);
foreach ($fields as $key => $value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string, '&');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->endpoints->authEndpoint->href);
curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($header));
curl_setopt($curl, CURLOPT_POST, count($fields));
curl_setopt($curl, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($curl);
if ($result===false) {
crest_error(curl_error($curl));
}
curl_close($curl);
$response=json_decode($result);
$this->access_token=$response->access_token;
$this->expiry=time()+$response->expires_in-20;
}
private function getEndpoints()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->urlbase);
curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($curl);
if ($result===false) {
crest_error(curl_error($curl));
}
curl_close($curl);
$response=json_decode($result);
return $response;
}
private function getRegionOrderUrls($regionid, $curl)
{
curl_setopt(
$curl,
CURLOPT_URL,
$this->endpoints->regions->href
.$regionid."/"
);
$result = curl_exec($curl);
if ($result===false) {
auth_error(curl_error($curl));
}
$response=json_decode($result);
return array($response->marketSellOrders->href,$response->marketBuyOrders->href);
}
public function getPriceData($regionid, $typeid)
{
$curl = curl_init();
if ($this->expiry<time()) {
$this->getAccessToken();
}
$auth_header='Authorization: Bearer '.$this->access_token;
$accept_header='Accept: application/vnd.ccp.eve.MarketOrderCollection-v1+json';
curl_setopt($curl, CURLOPT_USERAGENT, $this->useragent);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($auth_header,$accept_header));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
list($sellurl, $buyurl)=$this->getRegionOrderUrls($regionid, $curl);
$buy=$this->processPrice($curl, $typeid, $buyurl);
$sell=$this->processPrice($curl, $typeid, $sellurl);
curl_close($curl);
return json_encode(array("buy"=>$buy,"sell"=>$sell));
}
private function processPrice($curl, $typeid, $regionurl)
{
curl_setopt(
$curl,
CURLOPT_URL,
$regionurl."?type="
.$this->endpoints->itemTypes->href
.$typeid."/"
);
$result = curl_exec($curl);
if ($result===false) {
auth_error(curl_error($curl));
}
$response=json_decode($result);
$details=array();
foreach ($response->items as $item) {
$details[]=array(
"volume"=>$item->volume,
"minVolume"=>$item->minVolume,
"range"=>$item->range,
"location"=>$item->location->href,
"duration"=>$item->duration,
"buy"=>$item->buy,
"price"=>$item->price,
"issued"=>$item->issued
);
}
return $details;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment