Last active
March 1, 2024 22:10
-
-
Save xproot/6bbffba44f5a23de70d66bf82e0029cd to your computer and use it in GitHub Desktop.
Class for sending Discord webhooks with/out Rich embeds using PHP cURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
@Project: Discord Webhook class for PHP | |
@Author: xproot | |
@Contact: itsxproot at aol dot com | |
@Created: 5:48 PM Friday, January 26, 2024 | |
*/ | |
class DiscordWebhookMessage { | |
public $content = "Default message content"; | |
public $username = "Webhook"; | |
public $avatar_url = "https://ui-avatars.com/api?name=Webhook"; | |
public $tts = false; | |
public $file = ""; | |
public $embeds = []; | |
public $flags = 0; | |
public function addEmbed($embed) { | |
array_push($this->embeds, $embed); | |
} | |
public function send($webhookurl) { | |
$webhookclient = curl_init($webhookurl."?wait=true"); | |
if (empty($this->file)) | |
curl_setopt($webhookclient, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
else | |
curl_setopt($webhookclient, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data')); | |
curl_setopt($webhookclient, CURLOPT_POST, 1); | |
curl_setopt($webhookclient, CURLOPT_POSTFIELDS, json_encode($this)); | |
curl_setopt($webhookclient, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($webhookclient, CURLOPT_HEADER, 0); | |
curl_setopt($webhookclient, CURLOPT_RETURNTRANSFER, true); | |
$curloutput = curl_exec($webhookclient); | |
$httpstatuscode = curl_getinfo($webhookclient, CURLINFO_HTTP_CODE); | |
if (!str_starts_with((string)$httpstatuscode, "2")) | |
throw new Exception((string)$httpstatuscode); | |
curl_close($webhookclient); | |
return $curloutput; | |
} | |
public function encode() { | |
return json_encode($this); | |
} | |
} | |
class DiscordEmbed { | |
public $title = "Default Embed Title"; | |
public $description = "Default Description"; | |
public $url = "http://example.com"; | |
public $color = 0; | |
public $fields = []; | |
//public $author = []; these wont work unless json_encode outputs {} | |
//public $footer = []; | |
public $timestamp = "2024-01-26T20:26:45.000Z"; | |
//public $image = []; | |
//public $thumbnail = []; | |
public function addField($fieldname, $fieldvalue, $fieldinline = false) { | |
$tempfieldarray = array(); | |
$tempfieldarray['name'] = $fieldname; | |
$tempfieldarray['value'] = $fieldvalue; | |
if ($fieldinline) | |
$tempfieldarray['inline'] = true; | |
array_push($this->fields, $tempfieldarray); | |
} | |
public function addAuthor($authorname = "", $authorurl = "", $authoriconurl = "") { | |
$tempauthorarray = array(); | |
if (strlen($authorname) > 0) | |
$tempauthorarray['name'] = $authorname; | |
if (strlen($authorurl) > 0) | |
$tempauthorarray['url'] = $authorurl; | |
if (strlen($authoriconurl) > 0) | |
$tempauthorarray['icon_url'] = $authoriconurl; | |
$this->author = $tempauthorarray; | |
} | |
public function addFooter($footertext = "", $footericonurl = "") { | |
$tempfooterarray = array(); | |
if (strlen($footertext) > 0) | |
$tempfooterarray['text'] = $footertext; | |
if (strlen($footericonurl) > 0) | |
$tempfooterarray['icon_url'] = $footericonurl; | |
$this->footer = $tempfooterarray; | |
} | |
public function addImage($url) { | |
$this->image = array("url" => $url); | |
} | |
public function addThumbnail($url) { | |
$this->thumbnail = array("url" => $url); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example