-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDiscord.php
More file actions
160 lines (136 loc) · 6.65 KB
/
Copy pathDiscord.php
File metadata and controls
160 lines (136 loc) · 6.65 KB
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
<?php
// Discord-PHP Library
// Developed by Marco Cusano
// Version: 2.0
// Since 2016 for the Wumpus
namespace Discord {
class Endpoints {
function __construct($version = "v8") {
$this->methods = array(
"API" => array(
"root" => "https://discord.com/api/$version",
"channels" => "/channels",
"guilds" => "/guilds",
"invites" => "/invites",
"users" => "/users"
),
"CDN" => array(
"root" => "https://cdn.discord.com",
"applications" => array(
"icons" => "/app-icons/{id}/{value}.png",
"assets" => "/app-assets/{id}/{value}.png",
),
"avatars" => array(
"gif" => "/avatars/{id}/{value}.gif",
"png" => "/avatars/{id}/{value}.png"
),
"emojis" => "/emojis/{value}.png",
"guilds" => array(
"banners" => "/banners/{id}/{value}.png",
"icons" => "/icons/{id}/{value}.png",
"spashes" => "/splashes/{id}/{value}.png"
)
)
);
}
function get($levels, $base = "API") {
if (is_array($levels)) {
if (count($levels) == 2) { $endpoint = $this->methods[$base][$levels[0]][$levels[1]]; } else { $endpoint = $this->methods[$base][$levels[0]]; }
} else { $endpoint = $this->methods[$base][$levels]; }
if ($endpoint) {
if ($levels == "root") { return $endpoint; } else { return $this->methods[$base]["root"] . $endpoint; }
} else { return null; }
}
}
class Requests {
function __construct($authKey, $authType = "Bot", $contentType = "application/json") {
$this->authentication = $authType . ' ' . $authKey;
if ($contentType == "OAUTH2") {
$this->HTTP_HEADER = array('Content-Type: application/x-www-form-urlencoded');
} else if (is_numeric($contentType)) {
$this->HTTP_HEADER = array(
'Authorization: ' . $this->authentication,
'Content-Type: application/json',
'Content-length: ' . $contentType
);
} else {
$this->HTTP_HEADER = array(
'Authorization: ' . $this->authentication,
'Content-Type: application/json'
);
}
}
function get($endpoint, $responseType = "JSON") {
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->HTTP_HEADER);
curl_setopt_array($ch, array(
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => 0,
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$headers = explode("\n", $headers);
foreach ($headers as $header) {
list($key, $value) = explode(':', $header, 2);
$headers[trim($key)] = trim($value);
}
$body = substr($response, $header_size);
curl_close($ch);
// Return
if ($responseType == "JSON") {
return json_decode($body);
} else if ($responseType == "DATA" || $responseType == "BODY" || $responseType == "CONTENT") {
return $body;
} else if ($responseType == "HEAD" || $responseType == "HEADERS") {
return $headers;
}
}
function post($endpoint, $parameters, $method = "POST", $POST_FIELDS = "JSON", $responseType = "JSON") {
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->HTTP_HEADER);
curl_setopt_array($ch, array(
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_VERBOSE => 1,
CURLOPT_SSL_VERIFYPEER => 0,
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if ($POST_FIELDS == "QUERY") {
// BUILD QUERY (Like a Get)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters));
} else if ($POST_FIELDS == "JSON") {
// JSON ENCODE (Like a Post)
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($parameters));
} else {
return null;
}
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$headers = explode("\n", $headers);
foreach ($headers as $header) {
list($key, $value) = explode(':', $header, 2);
$headers[trim($key)] = trim($value);
}
$body = substr($response, $header_size);
curl_close($ch);
// Return
if ($responseType == "JSON") {
return json_decode($body);
} else if ($responseType == "DATA" || $responseType == "BODY" || $responseType == "CONTENT") {
return $body;
} else if ($responseType == "HEAD" || $responseType == "HEADERS") {
return $headers;
}
}
}
class Errors {
}
}
?>