This repository was archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTP.php
More file actions
97 lines (70 loc) · 2.06 KB
/
Copy pathHTTP.php
File metadata and controls
97 lines (70 loc) · 2.06 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
<?php
class HTTP {
private static function log ($msg) {
if (DEBUG) echo 'HTTP > ' . $msg . PHP_EOL;
}
private static function curl ($url) {
$curl = curl_init($url);
curl_setopt_array($curl, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_USERAGENT => 'HighlightBot/1.0',
]);
return $curl;
}
private static function execute ($curl) {
$raw = curl_exec($curl);
$response = new stdClass();
if (!curl_errno($curl)) {
$response->success = true;
$response->json = json_decode($raw, true);
$response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
} else {
$response->success = false;
$response->error = curl_error($curl);
$response->errno = curl_errno($curl);
}
curl_close($curl);
return $response;
}
public static function GET ($uri, $params) {
$params = http_build_query($params);
$requestURL = $uri . '?' . $params;
self::log("GET $requestURL");
$curl = self::curl($requestURL);
$response = self::execute($curl);
if ($response->success) {
return $response->json;
} else {
self::log("GET $requestURL - Error (" . $response->errno . ') "' . $response->error . '"');
return false;
}
}
public static function POST ($uri, $params) {
$requestURL = $uri;
self::log("POST $requestURL");
$curl = self::curl($requestURL);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$response = self::execute($curl);
if ($response->success) {
return $response->json;
} else {
self::log("POST $requestURL - Error (" . $response->errno . ') "' . $response->error . '"');
return false;
}
}
public static function DELETE ($uri, $params) {
$params = http_build_query($params);
$requestURL = $uri . '?' . $params;
self::log("DELETE $requestURL");
$curl = self::curl($requestURL);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
$response = self::execute($curl);
if ($response->success) {
return $response->json;
} else {
self::log("DELETE $requestURL - Error (" . $response->errno . ') "' . $response->error . '"');
return false;
}
}
}