-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAntpool.php
134 lines (120 loc) · 3.5 KB
/
Antpool.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
*** original antpool-php-api release
* @author Sebastian Lutz <lutz@baebeca.de>
* @copyright Baebeca Solutions
* @email lutz@baebeca.de
* @pgp 0x5AD0240C
* @link https://www.baebeca.de
* @link-github https://github.com/Elompenta/antpool-php-api
* @project antpool-php-api
* @license GNU GENERAL PUBLIC LICENSE Version 2
*
*** forked laravel-antpool-api
* @package aburakovskiy\laravel-antpool-api
* @author Alexander Burakovskiy <alexander.burakovskiy@gmail.com>
*/
namespace Aburakovskiy\LaravelAntpoolApi;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
class Antpool
{
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $key;
/**
* @var string
*/
protected $secret;
/**
* Constructor
* $antpool = new Antpool(ANTPOOL_USERNAME, ANTPOOL_API_KEY, ANTPOOL_API_SECRET)
*
* @param string $username
* @param string $key
* @param string $secret
* @throws \Exception
*/
public function __construct($username, $key, $secret)
{
if (!function_exists('curl_exec')) {
throw new \Exception("Error: Please install PHP curl extension to use this lib.");
}
$this->username = $username;
$this->key = $key;
$this->secret = $secret;
}
/**
* Test if the type can support the pageSize parameter
*
* @param $type
* @return bool
*/
function hasPageSizeParameter($type) {
return $type === 'workers' || $type === 'paymentHistory';
}
/**
* Make API call
*
* @param string $type
* @param string $coin BTC, LTC, ETH, ZEC, DAS
* @param int $page_size default 10
* @param int $page
* @return mixed
* @throws \Exception
*/
public function get($type, $coin = 'BTC', $page_size = 10, $page = 1)
{
$nonce = time();
$hmac_message = $this->username . $this->key . $nonce;
$hmac = strtoupper(hash_hmac('sha256', $hmac_message, $this->secret, false));
$post_fields = array(
'key' => $this->key,
'nonce' => $nonce,
'signature' => $hmac,
'coin' => $coin,
'page' => $page,
);
if($this->hasPageSizeParameter($type))
$post_fields = array_merge( $post_fields, array('pageSize' => $page_size));
$post_data = '';
foreach ($post_fields as $key => $value) {
$post_data .= $key . '=' . $value . '&';
}
rtrim($post_data, '&');
return $this->api_get($type, $post_fields, $post_data);
}
/*
* Internally used Methods, set visibility to public to enable more flexibility
*/
/**
* Using CURL to issue a GET request
*
* @param string $type
* @param array $post_fields
* @param string $post_data
* @return mixed
* @throws \Exception|\GuzzleHttp\Exception\GuzzleException
*/
public function api_get($type, $post_fields, $post_data)
{
$client = new Client();
$response = json_decode($client->request(
'POST',
'https://antpool.com/api/' . $type . '.htm?'.$post_data,
["headers" => [
'Accept' => 'application/json',
]]
)->getBody()->getContents());
if ($response->message === 'ok') {
return $response->data;
} else {
throw new \Exception('API Error: ' . print_r($response, true));
}
}
}