-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a8c5e68
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); |