1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
//---------------------------------------------------------------------- |
4
|
|
|
// |
5
|
|
|
// Copyright (C) 2015 Artem Rodygin |
6
|
|
|
// |
7
|
|
|
// This file is part of Linode API Client Library for PHP. |
8
|
|
|
// |
9
|
|
|
// You should have received a copy of the MIT License along with |
10
|
|
|
// the library. If not, see <http://opensource.org/licenses/MIT>. |
11
|
|
|
// |
12
|
|
|
//---------------------------------------------------------------------- |
13
|
|
|
|
14
|
|
|
namespace Linode; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Basic class which can make calls to Linode API. |
18
|
|
|
*/ |
19
|
|
|
class BaseLinodeApi |
20
|
|
|
{ |
21
|
|
|
/** @var string API key */ |
22
|
|
|
protected $key; |
23
|
|
|
|
24
|
|
|
/** @var \Linode\Batch */ |
25
|
|
|
protected $batch; |
26
|
|
|
|
27
|
|
|
/** @var array Additional cURL options. */ |
28
|
|
|
protected $options; |
29
|
|
|
|
30
|
|
|
/** @var bool Whether the object is in debug mode */ |
31
|
|
|
protected $debug; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Constructor. |
35
|
|
|
* |
36
|
|
|
* @param string|Batch $key API key, or batch of requests |
37
|
|
|
* @param array $options Additional cURL options to be set. |
38
|
|
|
* @param bool $debug Whether the object should be in debug mode |
39
|
|
|
*/ |
40
|
77 |
|
public function __construct($key, array $options = [], $debug = false) |
41
|
|
|
{ |
42
|
77 |
|
if ($key instanceof Batch) { |
43
|
1 |
|
$this->batch = $key; |
44
|
|
|
} |
45
|
|
|
else { |
46
|
76 |
|
$this->key = $key; |
47
|
76 |
|
$this->batch = null; |
48
|
|
|
} |
49
|
|
|
|
50
|
77 |
|
$this->options = $options; |
51
|
77 |
|
$this->debug = $debug; |
52
|
77 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Performs specified call to Linode API. |
56
|
|
|
* |
57
|
|
|
* @param string $action |
58
|
|
|
* @param array $parameters |
59
|
|
|
* |
60
|
|
|
* @throws LinodeException |
61
|
|
|
* @throws \Exception |
62
|
|
|
* |
63
|
|
|
* @return array|bool |
64
|
|
|
*/ |
65
|
77 |
|
protected function call($action, array $parameters = []) |
66
|
|
|
{ |
67
|
77 |
|
if ($this->batch) { |
68
|
|
|
|
69
|
1 |
|
$query = ['api_action' => $action]; |
70
|
|
|
|
71
|
1 |
|
foreach ($parameters as $key => $value) { |
72
|
1 |
|
if ($value !== null) { |
73
|
1 |
|
$query[urlencode($key)] = urlencode($value); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
$this->batch->addRequest($query); |
78
|
|
|
|
79
|
1 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
76 |
|
$curl = curl_init(); |
83
|
|
|
|
84
|
76 |
|
if (!$curl) { |
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
76 |
|
$query = "api_key={$this->key}&api_action={$action}"; |
89
|
|
|
|
90
|
76 |
|
foreach ($parameters as $key => $value) { |
91
|
71 |
|
if ($value !== null) { |
92
|
71 |
|
$query .= sprintf('&%s=%s', urlencode($key), urlencode($value)); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
76 |
|
if ($this->debug) { |
97
|
75 |
|
return $query; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
$this->options[CURLOPT_URL] = 'https://api.linode.com/'; |
101
|
1 |
|
$this->options[CURLOPT_POST] = true; |
102
|
1 |
|
$this->options[CURLOPT_POSTFIELDS] = $query; |
103
|
1 |
|
$this->options[CURLOPT_RETURNTRANSFER] = true; |
104
|
1 |
|
$this->options[CURLOPT_SSLVERSION] = CURL_SSLVERSION_TLSv1_2; |
105
|
|
|
|
106
|
1 |
|
curl_setopt_array($curl, $this->options); |
107
|
|
|
|
108
|
1 |
|
$result = curl_exec($curl); |
109
|
|
|
|
110
|
1 |
|
curl_close($curl); |
111
|
|
|
|
112
|
1 |
|
$json = json_decode($result, true); |
113
|
|
|
|
114
|
1 |
|
if (!$json) { |
115
|
|
|
throw new \RuntimeException('Empty response'); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
$error = reset($json['ERRORARRAY']); |
119
|
|
|
|
120
|
1 |
|
if ($error) { |
121
|
|
|
throw new LinodeException($error['ERRORMESSAGE'], $error['ERRORCODE']); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
return $json['DATA']; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|