A simple and elegant PHP library for sending messages to Discord via webhooks.
- PHP 8.0 or higher
- cURL extension enabled
- Composer
Install via Composer:
composer require mhhidayat/php-webhook-discorduse Mhhidayat\PhpWebhookDiscord\DiscordWebhook;
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Hello, Discord!')
->send();Use the fluent embed builder for creating rich embeds easily:
use Mhhidayat\PhpWebhookDiscord\DiscordWebhook;
use Mhhidayat\PhpWebhookDiscord\Contract\EmbedsContract;
use Mhhidayat\PhpWebhookDiscord\Enum\Colors;
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Check out this embed!')
->addEmbeds(function (EmbedsContract $embed) {
$embed->title('Embed Title')
->description('This is an embed description')
->color(Colors::Blue)
->url('https://example.com')
->enableTimestamp()
->fields([
[
'name' => 'Field 1',
'value' => 'Value 1',
'inline' => true
],
[
'name' => 'Field 2',
'value' => 'Value 2',
'inline' => true
]
]);
})
->send();You can still use raw arrays for custom content:
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->setContent([
'content' => 'Check out this embed!',
'embeds' => [
[
'title' => 'Embed Title',
'description' => 'This is an embed description',
'color' => 3447003,
'fields' => [
[
'name' => 'Field 1',
'value' => 'Value 1',
'inline' => true
],
[
'name' => 'Field 2',
'value' => 'Value 2',
'inline' => true
]
]
]
]
])
->send();DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->setUsername('Custom Bot Name')
->setAvatar('https://example.com/avatar.png')
->text('Message with custom appearance')
->send();DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('This message will be read aloud')
->allowTTS()
->send();The embed builder provides a fluent interface for creating rich embeds:
use Mhhidayat\PhpWebhookDiscord\DiscordWebhook;
use Mhhidayat\PhpWebhookDiscord\Contract\EmbedsContract;
use Mhhidayat\PhpWebhookDiscord\Enum\Colors;
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Server Status Update')
->setUsername('Status Bot')
->setAvatar('https://example.com/bot-avatar.png')
->addEmbeds(function (EmbedsContract $embed) {
$embed->title('System Status')
->description('All systems are operational')
->color(Colors::Green)
->url('https://status.example.com')
->enableTimestamp()
->authorName('System Monitor')
->authorUrl('https://example.com')
->authorIconUrl('https://example.com/icon.png')
->footerText('Powered by PHP Discord Webhook')
->footerIconUrl('https://example.com/footer-icon.png')
->imageUrl('https://example.com/image.png');
->imageHeight(10);
->imageWidth(10);
->thumbnailUrl('https://example.com/thumbnail.png');
->thumbnailWidth(10);
->thumbnailHeight(10);
->fields([
[
'name' => 'CPU Usage',
'value' => '45%',
'inline' => true
],
[
'name' => 'Memory',
'value' => '2.1GB / 8GB',
'inline' => true
],
[
'name' => 'Uptime',
'value' => '15 days',
'inline' => true
]
]);
})
->send();The library includes a comprehensive Colors enum with Discord's official colors:
use Mhhidayat\PhpWebhookDiscord\Enum\Colors;
// Discord Embed Colors
Colors::Default
Colors::Aqua
Colors::DarkAqua
Colors::Green
Colors::DarkGreen
Colors::Blue
Colors::DarkBlue
Colors::Purple
Colors::DarkPurple
Colors::LuminousVividPink
Colors::DarkVividPink
Colors::Gold
Colors::DarkGold
Colors::Orange
Colors::DarkOrange
Colors::Red
Colors::DarkRed
Colors::Grey
Colors::DarkGrey
Colors::DarkerGrey
Colors::LightGrey
Colors::Navy
Colors::DarkNavy
Colors::Yellow
// Official Discord Palette
Colors::White
Colors::Greyple
Colors::Black
Colors::DarkButNotBlack
Colors::Blurple
Colors::DiscordYellow
Colors::Fuchsia
// You can also use custom integer colors
->color(16711680) // Custom red colorSend messages only when a condition is met:
$webhook = DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Error occurred!')
->sendWhen($errorOccurred); // boolean
// Or with a closure
$webhook = DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Error occurred!')
->sendWhen(function() {
return someCondition();
});DiscordWebhook::withHeaders([
'Content-Type: application/json',
'User-Agent: MyApp/1.0'
])
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Message with custom headers')
->send();Default timeout is 15 seconds. You can customize it:
DiscordWebhook::timeout(30)
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Message with 30 second timeout')
->send();DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->setContent(function() {
return [
'content' => 'Dynamic message at ' . date('Y-m-d H:i:s'),
'embeds' => [
[
'title' => 'Server Status',
'description' => 'All systems operational'
]
]
];
})
->send();$webhook = DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Test message')
->send();
if ($webhook->successful()) {
echo "Message sent successfully!";
}
if ($webhook->failed()) {
echo "Failed to send message";
echo $webhook->getResponseJson();
}Create a new instance of DiscordWebhook.
Set the Discord webhook URL (required).
Set a simple text message (max 2000 characters).
Set custom content including embeds. Accepts an array or closure that returns an array.
Add rich embeds using the fluent EmbedsContract builder. The closure receives an EmbedsContract instance.
Override the default webhook username.
Override the default webhook avatar.
Enable text-to-speech for the message.
Send the webhook message.
Send the message only if the condition is true.
Check if the last request was successful (HTTP 2xx).
Check if the last request failed.
Get the raw JSON response from Discord.
Create instance with custom HTTP headers.
Create instance with custom timeout (default: 15 seconds).
The EmbedsContract provides these methods for building rich embeds:
Set the embed title.
Set the embed description.
Set the embed URL (makes the title clickable).
Set the embed color using the Colors enum or a custom integer.
Add the current timestamp to the embed.
Set the author name.
Set the author URL (makes the author name clickable).
Set the author icon URL.
Set the footer text.
Set the footer icon URL.
Add fields to the embed (max 10 fields). Each field should have 'name', 'value', and optionally 'inline' keys.
The library throws DiscordWebhookException for validation errors:
use Mhhidayat\PhpWebhookDiscord\DiscordWebhook;
use Mhhidayat\PhpWebhookDiscord\Exception\DiscordWebhookException;
try {
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text(str_repeat('a', 2001)) // Too long!
->send();
} catch (DiscordWebhookException $e) {
echo "Error: " . $e->getMessage();
}Common validation errors:
- Text exceeding 2000 characters
- More than 10 fields in an embed
- Missing webhook URL
- Missing content (no text or setContent called)
use Mhhidayat\PhpWebhookDiscord\DiscordWebhook;
use Mhhidayat\PhpWebhookDiscord\Contract\EmbedsContract;
use Mhhidayat\PhpWebhookDiscord\Enum\Colors;
function sendNotification($message, $level = 'info') {
$colors = [
'info' => Colors::Blue,
'success' => Colors::Green,
'warning' => Colors::Gold,
'error' => Colors::Red
];
$icons = [
'info' => 'βΉοΈ',
'success' => 'β
',
'warning' => 'β οΈ',
'error' => 'π¨'
];
DiscordWebhook::make()
->setWebhookURL($_ENV['DISCORD_WEBHOOK_URL'])
->addEmbeds(function (EmbedsContract $embed) use ($message, $level, $colors, $icons) {
$embed->title($icons[$level] . ' ' . strtoupper($level))
->description($message)
->color($colors[$level] ?? Colors::Blue)
->enableTimestamp();
})
->send();
}
sendNotification('User registration completed', 'success');
sendNotification('Database backup failed', 'error');use Mhhidayat\PhpWebhookDiscord\DiscordWebhook;
use Mhhidayat\PhpWebhookDiscord\Contract\EmbedsContract;
use Mhhidayat\PhpWebhookDiscord\Enum\Colors;
function logError($exception) {
DiscordWebhook::make()
->setWebhookURL($_ENV['DISCORD_WEBHOOK_URL'])
->setUsername('Error Logger')
->addEmbeds(function (EmbedsContract $embed) use ($exception) {
$embed->title('π¨ Error Occurred')
->description('An exception was thrown in the application')
->color(Colors::Red)
->enableTimestamp()
->fields([
[
'name' => 'Message',
'value' => $exception->getMessage(),
'inline' => false
],
[
'name' => 'File',
'value' => $exception->getFile(),
'inline' => true
],
[
'name' => 'Line',
'value' => (string) $exception->getLine(),
'inline' => true
]
])
->footerText('Error Logger v1.0');
})
->send();
}use Mhhidayat\PhpWebhookDiscord\DiscordWebhook;
use Mhhidayat\PhpWebhookDiscord\Contract\EmbedsContract;
use Mhhidayat\PhpWebhookDiscord\Enum\Colors;
function notifyDeployment($version, $environment, $author) {
DiscordWebhook::make()
->setWebhookURL($_ENV['DISCORD_WEBHOOK_URL'])
->setUsername('Deploy Bot')
->addEmbeds(function (EmbedsContract $embed) use ($version, $environment, $author) {
$embed->title('π New Deployment')
->description("Version $version has been deployed to $environment")
->color(Colors::Blurple)
->enableTimestamp()
->authorName($author)
->fields([
[
'name' => 'Version',
'value' => $version,
'inline' => true
],
[
'name' => 'Environment',
'value' => $environment,
'inline' => true
]
])
->footerText('Automated Deployment System');
})
->send();
}
notifyDeployment('v2.1.0', 'production', 'John Doe');See LICENSE.md for details.
mhhidayat - mhhidayat811@gmail.com