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 bool Whether the object is in debug mode */ |
28
|
|
|
protected $debug; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param string|Batch $key API key, or batch of requests |
34
|
|
|
* @param bool $debug Whether the object should be in debug mode |
35
|
|
|
*/ |
36
|
72 |
|
public function __construct($key, $debug = false) |
37
|
|
|
{ |
38
|
72 |
|
if ($key instanceof Batch) { |
39
|
1 |
|
$this->batch = $key; |
40
|
1 |
|
} |
41
|
|
|
else { |
42
|
71 |
|
$this->key = $key; |
43
|
71 |
|
$this->batch = null; |
44
|
|
|
} |
45
|
|
|
|
46
|
72 |
|
$this->debug = $debug; |
47
|
72 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Performs specified call to Linode API. |
51
|
|
|
* |
52
|
|
|
* @param string $action |
53
|
|
|
* @param array $parameters |
54
|
|
|
* |
55
|
|
|
* @return array|bool |
56
|
|
|
* |
57
|
|
|
* @throws LinodeException |
58
|
|
|
* @throws \Exception |
59
|
|
|
*/ |
60
|
72 |
|
protected function call($action, array $parameters = []) |
61
|
|
|
{ |
62
|
72 |
|
if ($this->batch) { |
63
|
|
|
|
64
|
1 |
|
$query = ['api_action' => $action]; |
65
|
|
|
|
66
|
1 |
|
foreach ($parameters as $key => $value) { |
67
|
1 |
|
if ($value !== null) { |
68
|
1 |
|
$query[urlencode($key)] = urlencode($value); |
69
|
1 |
|
} |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
$this->batch->addRequest($query); |
73
|
|
|
|
74
|
1 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
71 |
|
$curl = curl_init(); |
78
|
|
|
|
79
|
71 |
|
if (!$curl) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
71 |
|
$query = "api_key={$this->key}&api_action={$action}"; |
84
|
|
|
|
85
|
71 |
|
foreach ($parameters as $key => $value) { |
86
|
66 |
|
if ($value !== null) { |
87
|
60 |
|
$query .= sprintf('&%s=%s', urlencode($key), urlencode($value)); |
88
|
60 |
|
} |
89
|
71 |
|
} |
90
|
|
|
|
91
|
71 |
|
if ($this->debug) { |
92
|
70 |
|
return $query; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
curl_setopt($curl, CURLOPT_URL, 'https://api.linode.com/'); |
96
|
1 |
|
curl_setopt($curl, CURLOPT_POST, true); |
97
|
1 |
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $query); |
98
|
1 |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
99
|
|
|
|
100
|
1 |
|
$result = curl_exec($curl); |
101
|
|
|
|
102
|
1 |
|
curl_close($curl); |
103
|
|
|
|
104
|
1 |
|
$json = json_decode($result, true); |
105
|
|
|
|
106
|
1 |
|
if (!$json) { |
107
|
|
|
throw new \RuntimeException('Empty response'); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$error = reset($json['ERRORARRAY']); |
111
|
|
|
|
112
|
1 |
|
if ($error) { |
113
|
|
|
throw new LinodeException($error['ERRORMESSAGE'], $error['ERRORCODE']); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
return $json['DATA']; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|