A Discord webhook is a tool that allows you to automate sending messages and notifications to a Discord channel directly from your application or website. It provides a unique URL that you can use to post content without requiring a Discord bot.
Webhooks are commonly used to send updates, alerts, or integrate with other services like GitHub, Jenkins, or custom scripts. By sending a JSON payload to the webhook URL, you can customize the message, include embeds, and even mention users or roles, making it a powerful tool for real-time communication within your Discord server.
And with a simple snippet of code, it’s easy to do and integrate into any of your PHP applications. (just make sure you have the cURL extension installed)
function sendDiscordWebhook(array $payload, string $webhookUrl): bool {
// Check if the webhook URL is valid
if (filter_var($webhookUrl, FILTER_VALIDATE_URL) === false) {
error_log('Invalid webhook URL.');
return false;
}
// Ensure the payload contains required fields
if (!isset($payload['content']) || empty($payload['content'])) {
error_log('The payload must contain a "content" field.');
return false;
}
// Encode the payload as JSON
try {
$jsonPayload = json_encode($payload, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
error_log('JSON encoding error: ' . $e->getMessage());
return false;
}
// Initialize cURL
$ch = curl_init($webhookUrl);
if ($ch === false) {
error_log('Failed to initialize cURL.');
return false;
}
// Set cURL options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the request
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
// Close cURL
curl_close($ch);
// Error handling
if ($response === false) {
error_log('cURL error: ' . $curlError);
return false;
}
if ($httpCode !== 204) {
error_log("Unexpected HTTP code: $httpCode. Response: $response");
return false;
}
return true;
}
There are options which allow you to embed fields, set the avatar and also checking that the Discord server returns the correct response code (204) on a successful delivery.
You’ll need to view properties in the channel and create the webhook user, this will allow you to set the Webhook Bot username and it’ll also give you a unique webhook URL to POST to which allows the sending of the message. Example usage below:
$webhookUrl = 'https://discord.com/api/webhooks/your_webhook_id/your_webhook_token';
$payload = [
'username' => 'My Custom Bot',
'avatar_url' => 'https://example.com/my-avatar.png',
'content' => 'Hello, Discord!',
'embeds' => [
[
'title' => 'Sample Embed',
'description' => 'This is an embed description',
'color' => 16711680, // Red color
'fields' => [
[
'name' => 'Field 1',
'value' => 'This is the value for field 1',
'inline' => true
],
[
'name' => 'Field 2',
'value' => 'This is the value for field 2',
'inline' => true
]
]
]
]
];
if (sendDiscordWebhook($payload, $webhookUrl)) {
echo "Message sent successfully!";
} else {
echo "Failed to send message. Check the error logs for details.";
}
That’s all there is to it. Create the message payload object as above, choose your options and call the sendDiscordWebhook
and you should see it posted instantly in your selected channel. If this is your private Discord, you could make it mention yourself for real-time alerts on both mobile and desktop.