|
1
|
|
|
<?php |
|
2
|
|
|
namespace Sovereign\Lib; |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
use Monolog\Logger; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class cURL |
|
9
|
|
|
* @package Sovereign\Lib |
|
10
|
|
|
*/ |
|
11
|
|
|
class cURL |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var Logger |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $log; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* cURL constructor. |
|
20
|
|
|
* @param Logger $log |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(Logger $log) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->log = $log; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param String $url |
|
29
|
|
|
* @param array $headers |
|
30
|
|
|
* @return mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
public function get(String $url, $headers = array()) |
|
33
|
|
|
{ |
|
34
|
|
|
$headers = array_merge($headers, ["Connection: keep-alive", "Keep-Alive: timeout=10, max=1000"]); |
|
35
|
|
|
|
|
36
|
|
|
try { |
|
37
|
|
|
$curl = curl_init(); |
|
38
|
|
|
curl_setopt_array($curl, [ |
|
39
|
|
|
CURLOPT_USERAGENT => "Sovereign Discord Bot", |
|
40
|
|
|
CURLOPT_TIMEOUT => 8, |
|
41
|
|
|
CURLOPT_FORBID_REUSE => false, |
|
42
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
43
|
|
|
CURLOPT_FAILONERROR => true, |
|
44
|
|
|
CURLOPT_URL => $url, |
|
45
|
|
|
CURLOPT_POST => false, |
|
46
|
|
|
CURLOPT_HTTPHEADER => $headers |
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
|
|
$result = curl_exec($curl); |
|
50
|
|
|
|
|
51
|
|
|
return $result; |
|
52
|
|
|
} catch (\Exception $e) { |
|
53
|
|
|
$this->log->addError("There was an error using cURL: ", [$e->getMessage()]); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param String $url |
|
59
|
|
|
* @param array $parameters |
|
60
|
|
|
* @param array $headers |
|
61
|
|
|
* @return mixed |
|
62
|
|
|
*/ |
|
63
|
|
|
public function post(String $url, $parameters = array(), $headers = array()) |
|
64
|
|
|
{ |
|
65
|
|
|
$headers = array_merge($headers, ["Connection: keep-alive", "Keep-Alive: timeout=10, max=1000", "Content-Type: application/x-www-form-urlencoded"]); |
|
66
|
|
|
|
|
67
|
|
|
try { |
|
68
|
|
|
$curl = curl_init(); |
|
69
|
|
|
curl_setopt_array($curl, [ |
|
70
|
|
|
CURLOPT_USERAGENT => "Sovereign Discord Bot", |
|
71
|
|
|
CURLOPT_TIMEOUT => 8, |
|
72
|
|
|
CURLOPT_FORBID_REUSE => false, |
|
73
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
74
|
|
|
CURLOPT_FAILONERROR => true, |
|
75
|
|
|
CURLOPT_URL => $url, |
|
76
|
|
|
CURLOPT_POST => true, |
|
77
|
|
|
CURLOPT_HTTPHEADER => $headers, |
|
78
|
|
|
CURLOPT_POSTFIELDS => http_build_query($parameters) |
|
79
|
|
|
]); |
|
80
|
|
|
|
|
81
|
|
|
$result = curl_exec($curl); |
|
82
|
|
|
|
|
83
|
|
|
return $result; |
|
84
|
|
|
} catch (\Exception $e) { |
|
85
|
|
|
$this->log->addError("There was an error using cURL: ", [$e->getMessage()]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
} |