diff --git a/composer.json b/composer.json index 55b6619..9c98809 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "aburakovskiy/laravel-antpool-api", "description": "Antpool PHP API Client for Laravel", "type": "library", - "license": "GPL-2.0-or-later", + "license": "GPL", "authors": [ { "name": "Alexander Burakovskiy", @@ -11,7 +11,9 @@ ], "minimum-stability": "dev", "require": { - "php": ">=5.6" + "php": ">=5.6", + "ext-json": "*", + "guzzlehttp/guzzle": "^7.3" }, "autoload": { "psr-4": { diff --git a/src/Antpool.php b/src/Antpool.php index ee01f1e..26eec57 100644 --- a/src/Antpool.php +++ b/src/Antpool.php @@ -17,6 +17,9 @@ */ namespace Aburakovskiy\LaravelAntpoolApi; +use GuzzleHttp\Client; +use GuzzleHttp\RequestOptions; + class Antpool { /** @@ -68,10 +71,11 @@ function hasPageSizeParameter($type) { * @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) + public function get($type, $coin = 'BTC', $page_size = 10, $page = 1) { $nonce = time(); $hmac_message = $this->username . $this->key . $nonce; @@ -82,6 +86,7 @@ public function get($type, $coin = 'BTC', $page_size = 10) 'nonce' => $nonce, 'signature' => $hmac, 'coin' => $coin, + 'page' => $page, ); if($this->hasPageSizeParameter($type)) @@ -93,8 +98,7 @@ public function get($type, $coin = 'BTC', $page_size = 10) } rtrim($post_data, '&'); - $apiData = $this->api_get($type, $post_fields, $post_data); - return $apiData; + return $this->api_get($type, $post_fields, $post_data); } /* @@ -108,37 +112,23 @@ public function get($type, $coin = 'BTC', $page_size = 10) * @param array $post_fields * @param string $post_data * @return mixed - * @throws \Exception + * @throws \Exception|\GuzzleHttp\Exception\GuzzleException */ public function api_get($type, $post_fields, $post_data) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, 'https://antpool.com/api/' . $type . '.htm'); - // todo: switch to public cert - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($ch, CURLOPT_POST, count($post_fields)); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - // set large timeout because API lak sometimes - curl_setopt($ch, CURLOPT_TIMEOUT, 60); - $result = curl_exec($ch); - curl_close($ch); - - // check if curl was timed out - if ($result === false) { - throw new \Exception('Error: No API connect'); - } - - // validate JSON - $result_json = json_decode($result, true); - if (json_last_error() != JSON_ERROR_NONE) { - throw new \Exception('Error: read broken JSON from API - JSON Error: ' . json_last_error() . ' (' . $result . ')'); - } + $client = new Client(); + $response = json_decode($client->request( + 'POST', + 'https://antpool.com/api/' . $type . '.htm?'.$post_data, + ["headers" => [ + 'Accept' => 'application/json', + ]] + )->getBody()->getContents()); - if ($result_json['message'] == 'ok') { - return $result_json['data']; + if ($response->message === 'ok') { + return $response->data; } else { - throw new \Exception('API Error: ' . print_r($result_json, true)); + throw new \Exception('API Error: ' . print_r($response, true)); } } } \ No newline at end of file