1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Appino\Blockchain\Classes; |
4
|
|
|
|
5
|
|
|
use Appino\Blockchain\Classes\Create; |
6
|
|
|
use Appino\Blockchain\Classes\Receive; |
7
|
|
|
use Appino\Blockchain\Classes\Wallet; |
8
|
|
|
use Appino\Blockchain\Exception\ApiError; |
9
|
|
|
use Appino\Blockchain\Exception\Error; |
10
|
|
|
use Appino\Blockchain\Exception\HttpError; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
use GuzzleHttp\Exception\GuzzleException; |
13
|
|
|
use Illuminate\Contracts\Container\Container; |
14
|
|
|
|
15
|
|
|
class Blockchain{ |
16
|
|
|
|
17
|
|
|
protected $client; |
18
|
|
|
protected $params; |
19
|
|
|
public $HD = false; |
20
|
|
|
const GET = 'GET'; |
21
|
|
|
const POST = 'POST'; |
22
|
|
|
|
23
|
|
|
public function __construct($config){ |
24
|
|
|
$this->client = new Client(['base_uri'=>data_get($config,'base_uri')]); |
25
|
|
|
$this->params['api_code'] = data_get($config,'api_code'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return Create |
30
|
|
|
*/ |
31
|
|
|
public function Create(){ |
32
|
|
|
return new Create($this); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return Wallet |
37
|
|
|
*/ |
38
|
|
|
public function Wallet(){ |
39
|
|
|
return new Wallet($this); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Receive |
44
|
|
|
*/ |
45
|
|
|
public function Receive(){ |
46
|
|
|
return new Receive($this); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param $base_uri string Default : http://localhost:3000 |
51
|
|
|
*/ |
52
|
|
|
|
53
|
|
|
public function newBaseUri($base_uri){ |
54
|
|
|
$this->client = new Client(['base_uri'=>$base_uri]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $method |
59
|
|
|
* @param $url |
60
|
|
|
* @param $params |
61
|
|
|
* @return array |
62
|
|
|
* @throws HttpError |
63
|
|
|
*/ |
64
|
|
|
|
65
|
|
|
public function Request($method, $url, $params){ |
66
|
|
|
$params = array_merge($params, $this->params); |
67
|
|
|
switch ($method){ |
68
|
|
|
case 'GET': |
69
|
|
|
case 'DELETE': |
70
|
|
|
$options = array('query'=>$params); |
71
|
|
|
break; |
72
|
|
|
case 'POST': |
73
|
|
|
$options = array( |
74
|
|
|
'form_params'=>$params, |
75
|
|
|
'headers'=>[ |
76
|
|
|
'Content-Type'=>'application/x-www-form-urlencoded', |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
try { |
82
|
|
|
$response = $this->client->request($method, $url, $options); |
|
|
|
|
83
|
|
|
$json = json_decode($response->getBody()->getContents(),true); |
84
|
|
|
if(is_null($json)) { |
85
|
|
|
// this is possibly a from btc request with a comma separation |
86
|
|
|
$json = json_decode(str_replace(',', '', $response)); |
87
|
|
|
if (is_null($json)) |
88
|
|
|
throw new Error("Unable to decode JSON response from Blockchain: " . $response->getBody()->getContents()); |
89
|
|
|
} |
90
|
|
|
if(array_key_exists('error', $json)) { |
91
|
|
|
throw new ApiError($json['error']); |
92
|
|
|
} |
93
|
|
|
return $json; |
94
|
|
|
} catch (GuzzleException $e) { |
95
|
|
|
echo $e->getMessage(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|