forked from mahdiMGF2/mirzabot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrequest.php
More file actions
107 lines (90 loc) · 2.76 KB
/
request.php
File metadata and controls
107 lines (90 loc) · 2.76 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
<?php
class CurlRequest {
private $url;
private $headers = [];
private $timeout = null;
private $authToken = null;
private $api_key = null;
private $cookie = null;
public function __construct($url) {
$this->url = $url;
}
public function setTimeout($seconds) {
$this->timeout = $seconds;
}
public function setHeaders(array $headers) {
$this->headers = array_merge($this->headers, $headers);
}
public function setBearerToken($token) {
$this->authToken = $token;
}
public function api_key($token) {
$this->api_key = $token;
}
public function setCookie($cookieStr) {
$this->cookie = $cookieStr;
}
private function prepareHeaders() {
$headers = $this->headers;
if ($this->authToken) {
$headers[] = "Authorization: Bearer {$this->authToken}";
}
if ($this->api_key) {
$headers[] = $this->authToken;
}
return $headers;
}
private function execute($method, $data = null) {
$this->timeout = !$this->timeout ? 8000 : $this->timeout;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$finalHeaders = $this->prepareHeaders();
if (!empty($finalHeaders)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $finalHeaders);
}
if ($this->cookie) {
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie);
}
if ($data) {
if (is_array($data)) {
$data = http_build_query($data);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$response = curl_exec($ch);
if (curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
return [
'status' => null,
'body' => null,
'error' => $error,
];
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return [
'status' => $httpCode,
'body' => $response
];
}
public function get() {
return $this->execute("GET");
}
public function post($data) {
return $this->execute("POST", $data);
}
public function put($data) {
return $this->execute("PUT", $data);
}
public function delete($data = null) {
return $this->execute("DELETE", $data);
}
public function PATCH($data = null){
return $this->execute('PATCH',$data);
}
}