Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
UnDeleteRU committed May 1, 2014
0 parents commit a8c5e68
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
112 changes: 112 additions & 0 deletions BTCEApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

class BTCEApi extends BTCEApiNoKey
{
private $apiKey;

private $apiSecret;

private $noOnce;

public function __construct($apiKey, $apiSecret)
{
$this->apiKey = $apiKey;
$this->apiSecret = $apiSecret;
$this->noOnce = time();
}

public function getFunds()
{
if (!$this->funds) {
$result = $this->apiQuery('getInfo');

if (!$result) {
throw new \Exception('Info not avalible');
}

$this->funds = $result['funds'];
}

return $this->funds;
}

public function orderTrade($pair, $type, $rate, $amount)
{
$data = array(
'pair' => $pair,
'type' => $type,
'rate' => $rate,
'amount' => $amount,
);

$response = $this->apiQuery('Trade', $data);

if ($response) {
return $response['order_id'];
}

return null;
}

public function cancelOrder($orderId)
{
$response = $this->apiQuery('CancelOrder', array(
'order_id' => $orderId,
));

return $response;
}

public function getActiveOrders()
{
return $this->apiQuery('ActiveOrders');
}

protected function getNoOnce()
{
return $this->noOnce++;
}

public function apiQuery($method, $req = array())
{
$req['method'] = $method;
$req['nonce'] = $this->getNoOnce();

// generate the POST data string
$post_data = http_build_query($req, '', '&');

// Generate the keyed hash value to post
$sign = hash_hmac("sha512", $post_data, $this->apiSecret);

// Add to the headers
$headers = array(
'Sign: '.$sign,
'Key: '.$this->apiKey,
);

$curl = curl_init('https://btc-e.com/tapi/');

curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $post_data,
));

$res = curl_exec($curl);

$result = json_decode($res, true);

if ($result['success']) {
if (isset($result['funds'])) {
$this->funds = $result['funds'];
}

return $result['return'];
}

return null;
}
}
41 changes: 41 additions & 0 deletions BTCEApiNoKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

class BTCEApiNoKey
{
protected function get($pair, $method)
{
$url = sprintf("https://btc-e.com/api/2/%s/%s", $pair, $method);

$curl = curl_init($url);

curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0
]);

$result = curl_exec($curl);

return json_decode($result, true);
}

public function getFee($pair)
{
return $this->get($pair, 'fee')['trade'];
}

public function getTicker($pair)
{
return $this->get($pair, 'ticker')['ticker'];
}

public function getTrades($pair)
{
return $this->get($pair, 'trades');
}

public function getDepth($pair)
{
return $this->get($pair, 'depth');
}
}
19 changes: 19 additions & 0 deletions Example/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

require_once __DIR__ . '/../BTCEApiNoKey.php';

$pair = 'btc_usd';
$api = new BTCEApiNoKey();

$fee = $api->getFee($pair);
$depth = $api->getDepth($pair);
$ticker = $api->getTicker($pair);
$trades = $api->getTrades($pair);

$firstAsk = $depth['asks'][0];
$firstBid = $depth['bids'][0];

printf("Pair %s fee: %.2f\n", $pair, $fee);
printf("First ask %.4f (%.4f) First bid %.4f (%.4f)\n", $firstAsk[0], $firstAsk[1], $firstBid[0], $firstBid[1]);
printf("High: %.4f Low: %.4f Last: %.4f\n", $ticker['high'], $ticker['low'], $ticker['last']);
printf("Last trade %s %.4f (%.4f)", $trades[0]['trade_type'], $trades[0]['price'], $trades[0]['amount']);

0 comments on commit a8c5e68

Please sign in to comment.