Skip to content

Instantly share code, notes, and snippets.

@xproot
Last active March 1, 2024 22:10
Show Gist options
  • Save xproot/6bbffba44f5a23de70d66bf82e0029cd to your computer and use it in GitHub Desktop.
Save xproot/6bbffba44f5a23de70d66bf82e0029cd to your computer and use it in GitHub Desktop.
Class for sending Discord webhooks with/out Rich embeds using PHP cURL
<?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);
}
}
@xproot
Copy link
Author

xproot commented Jan 26, 2024

Example

require_once('DiscordWebhook.php');
$dwebmsg = new DiscordWebhookMessage;
$dembone = new DiscordEmbed;
$dembone->addField("Field", "Field Value");
$dembone->addAuthor("xproot", "http://xproot.pw/", "https://gravatar.com/avatar/204a5e56b881de6401f1e971c4760ec002231c838a7541240b392a1a8732dd14");
$dembone->addFooter("footer", "https://ui-avatars.com/api?name=Footer");
$dembone->addImage("https://ui-avatars.com/api?name=Image");
$dembone->addThumbnail("https://ui-avatars.com/api?name=Thumbnail");
$dembone->timestamp = date("c");
$dwebmsg->addEmbed($dembone);
$dwebmsg->content = "It works!";
try {
	$dwebmsg->send("https://discord.com/api/webhooks/id/token");
} catch (Exception $ex) {
	echo "Discord Webhook returned: ".$ex->getMessage());
}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment