This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.buycraftAPI.php
171 lines (150 loc) · 4.2 KB
/
class.buycraftAPI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* A Buycraft interface for PHP
*
* @author Chris Ireland <[email protected]>
* @license MIT <http://opensource.org/licenses/MIT>
*/
class buycraftAPI
{
/**
* Your Buycraft salt
*
* @var string
*/
private $apiKey = '';
/**
* Url to the api
*
* @var string
*/
private $apiUrl = 'http://api.buycraft.net/v4?';
/**
* The format the api call should be returned as
*
* @var string
*/
private $apiFormat = 'string';
/**
* Create a new class instance
* - There are 3 available $apiFormats
* - 'string'|null : returns as a json document (default)
* - 'array' : returns as a php array
* - 'object' : returns as as a php object
*
* @param $apiKey
* @param string $apiFormat
* @throws Exception
*/
function __construct($apiKey, $apiFormat = 'string')
{
// Set class variables
$this->apiKey = $apiKey;
$this->apiFormat = $apiFormat;
// Validation format option
if ($this->apiFormat !== 'string' && $this->apiFormat !== 'object' && $this->apiFormat !== 'array' )
throw new Exception('Invalid API return format');
}
/**
* API command builder and executor
*
* @param $action
* @param null $do
* @param null $commands
* @param null $limit
* @return array|mixed|string
* @throws Exception
*/
protected function apiCommand($action, $do = null, $commands = null, $limit = null)
{
// Build the http query to the api
$apiQuery = array(
'secret' => $this->apiKey,
'action' => $action,
'do' => $do,
'commands' => $commands,
'limit' => $limit
);
$apiQuery = http_build_query($apiQuery);
// Query the api and fetch the response
$ch = curl_init($this->apiUrl . $apiQuery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$apiQuery = curl_exec($ch);
if($apiQuery === false || curl_getinfo($ch,CURLINFO_HTTP_CODE) !== 200) {
curl_close($ch);
throw new Exception('Buycraft API failed to respond correctly');
}
curl_close($ch);
// Handle formatting
if ($this->apiFormat === 'object') {
$apiQuery = json_decode($apiQuery);
} elseif ($this->apiFormat === 'array') {
$apiQuery = json_decode($apiQuery, true);
}
return $apiQuery;
}
/**
* Return a JSON document of all payments made to Buycraft
*
* @param null $limit
* @return array|mixed|string
* @throws Exception
*/
public function getPayments($limit = null)
{
return $this->apiCommand('payments', null, null, $limit);
}
/**
* Return a JSON document of all available store categories
*
* @return array|mixed|string
* @throws Exception
*/
public function getCategories()
{
return $this->apiCommand('categories');
}
/**
* Return a JSON document of all available store packages
*
* @return array|mixed|string
* @throws Exception
*/
public function getPackages()
{
return $this->apiCommand('packages');
}
/**
* Return a JSON document of pending players
*
* @param null $limit
* @return array|mixed|string
* @throws Exception
*/
public function getPendingPlayers($limit = null)
{
return $this->apiCommand('pendingUsers', null, null, $limit);
}
/**
* Return JSON document of pending commands
*
* @param null $limit
* @return array|mixed|string
* @throws Exception
*/
public function getPendingCommands($limit = null) {
return $this->apiCommand('commands', 'lookup', null, $limit);
}
/**
* Remove commands from the queue
*
* @param $commandIDs
* @return array|mixed|string
* @throws Exception
*/
public function removePendingCommands($commandIDs) {
if(!is_array($commandIDs))
throw new Exception('Command IDs must be defined as an array');
return $this->apiCommand('commands', 'removeId', json_encode($commandIDs));
}
}