-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.php
More file actions
89 lines (73 loc) · 1.88 KB
/
Client.php
File metadata and controls
89 lines (73 loc) · 1.88 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
<?php
namespace DiscordWebhooks;
/**
* Client generates the payload and sends the webhook payload to Discord
*/
class Client
{
protected $url;
protected $username;
protected $avatar;
protected $message;
protected $embeds;
protected $tts;
public function __construct($url)
{
$this->url = $url;
}
public function tts($tts = false) {
$this->tts = $tts;
return $this;
}
public function username($username)
{
$this->username = $username;
return $this;
}
public function avatar($new_avatar)
{
$this->avatar = $new_avatar;
return $this;
}
public function message($new_message)
{
$this->message = $new_message;
return $this;
}
public function embed($embed) {
$this->embeds[] = $embed->toArray();
return $this;
}
public function send()
{
$payload = json_encode(array(
'username' => $this->username,
'avatar_url' => $this->avatar,
'content' => $this->message,
'embeds' => $this->embeds,
'tts' => $this->tts,
));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
throw new \Exception("cURL error ({$errno}):\n {$error_message}");
}
$json_result = json_decode($result, true);
if (($httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE)) != 204)
{
throw new \Exception($httpcode . ':' . $result);
}
curl_close($ch);
return $this;
}
}
?>