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 1
/
class.wynncraftAPI.php
188 lines (166 loc) · 5.02 KB
/
class.wynncraftAPI.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* A Wynncraft API interface for PHP
*
* - Docs available for the API @ <http://docs.wynncraft.com/>
*
* @author Chris Ireland <[email protected]>
* @license MIT <http://opensource.org/licenses/MIT>
*/
class wynncraftAPI
{
/**
* Url to the api
*
* @const string
*/
const apiUrl = 'http://api.wynncraft.com/public_api.php?';
/**
* 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 string $apiFormat
* @throws Exception
*/
function __construct($apiFormat = 'string')
{
// Set class variables
$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 $command
* @param null $limit
* @return array|mixed|string
* @throws Exception
*/
protected function apiCommand($action, $command = null, $limit = null)
{
// Build the http query to the api
$apiQuery = array(
'action' => $action,
'command' => $command,
'limit' => $limit
);
$apiQuery = http_build_query($apiQuery);
// Query the api and fetch the response
$ch = curl_init(self::apiUrl . $apiQuery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$apiQuery = curl_exec($ch);
if($apiQuery === false || curl_getinfo($ch,CURLINFO_HTTP_CODE) !== 200)
throw new Exception('Wynncraft 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 with a player's stats
*
* @param $username
* @return array|mixed|string
* @throws Exception
*/
public function playerStats($username) {
return $this->apiCommand('playerStats', $username);
}
/**
* Return a JSON document detailing all servers and their players
*
* @return array|mixed|string
* @throws Exception
*/
public function onlinePlayers() {
return $this->apiCommand('onlinePlayers');
}
/**
* Return a JSON document with a sum of all online players
*
* @return array|mixed|string
* @throws Exception
*/
public function onlinePlayersSum() {
return $this->apiCommand('onlinePlayersSum');
}
/**
* Return a JSON document with pvp statistic data (max limit 100)
* - There are 3 $type options:
* - 'global'|null : returns all time stats (default)
* - 'daily' : returns today's stats
* - 'weekly' : returns this week's stats
*
* @param null $type
* @param null $limit
* @return array|mixed|string
* @throws Exception
*/
public function pvpLeaderboard($type = null, $limit = null) {
return $this->apiCommand('pvpLeaderboard', $type, $limit);
}
/**
* Return a JSON document with items based on a filter
* - There is a smart $filter:
* - An integer will search for min lvl
* - An item type will search for all items with that type
* - A quality will search search for all items with that quality
*
* @param string $filter
* @return array|mixed|string
* @throws Exception
*/
public function searchItems($filter) {
return $this->apiCommand('items', $filter);
}
/**
* Return a JSON document with a guild's stats
*
* @param $guild
* @return array|mixed|string
* @throws Exception
*/
public function guildStats($guild) {
return $this->apiCommand('guildStats', $guild);
}
/**
* Return a JSON document with a list of guilds
*
* @return array|mixed|string
* @throws Exception
*/
public function guildList()) {
return $this->apiCommand('guildList');
}
/**
* Return a JSON document with wf statistic data (max limit 100)
* - There are 3 $type options:
* - 'global'|null : returns all time stats (default)
* - 'daily' : returns today's stats
* - 'weekly' : returns this week's stats
*
* @param null $type
* @param null $limit
* @return array|mixed|string
* @throws Exception
*/
public function wfLeaderboard($type = null, $limit = null) {
return $this->apiCommand('wfLeaderboard', $type, $limit);
}
}