|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @link https://github.com/codenix-sv/bittrex-api |
|
5
|
|
|
* @copyright Copyright (c) 2017 codenix-sv |
|
6
|
|
|
* @license https://github.com/codenix-sv/bittrex-api/blob/master/LICENSE |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace codenixsv\Bittrex\Http; |
|
10
|
|
|
|
|
11
|
|
|
use codenixsv\Bittrex\Exceptions\CurlException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class CurlHttpClient |
|
15
|
|
|
* @package codenixsv\Bittrex\Http |
|
16
|
|
|
*/ |
|
17
|
|
|
class CurlHttpClient implements HttpClient |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @param $url |
|
21
|
|
|
* @param array $headers |
|
22
|
|
|
* @return mixed |
|
23
|
|
|
* @throws CurlException |
|
24
|
|
|
*/ |
|
25
|
|
|
public function get($url, array $headers = []) |
|
26
|
|
|
{ |
|
27
|
|
|
$ch = curl_init($url); |
|
28
|
|
|
if (!empty($headers)) { |
|
29
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
|
30
|
|
|
} |
|
31
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
32
|
|
|
$response = curl_exec($ch); |
|
33
|
|
|
|
|
34
|
|
|
$this->checkExceptions($ch); |
|
35
|
|
|
|
|
36
|
|
|
return $response; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param $url |
|
41
|
|
|
* @param array $parameters |
|
42
|
|
|
* @param array $headers |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
* @throws CurlException |
|
45
|
|
|
*/ |
|
46
|
|
|
public function post($url, array $parameters = [], array $headers = []) |
|
47
|
|
|
{ |
|
48
|
|
|
$ch = curl_init($url); |
|
49
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
50
|
|
|
curl_setopt($ch, CURLOPT_POST, true); |
|
51
|
|
|
|
|
52
|
|
|
if (!empty($headers)) { |
|
53
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (!empty($parameters)) { |
|
57
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$response = curl_exec($ch); |
|
61
|
|
|
|
|
62
|
|
|
$this->checkExceptions($ch); |
|
63
|
|
|
|
|
64
|
|
|
curl_close($ch); |
|
65
|
|
|
|
|
66
|
|
|
return $response; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param $ch |
|
71
|
|
|
* @throws CurlException |
|
72
|
|
|
*/ |
|
73
|
|
|
private function checkExceptions($ch) |
|
74
|
|
|
{ |
|
75
|
|
|
if (curl_errno($ch)) { |
|
76
|
|
|
$errorMessage = curl_error($ch); |
|
77
|
|
|
curl_close($ch); |
|
78
|
|
|
throw new CurlException($errorMessage); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|