Last active
September 2, 2024 14:57
-
Star
(132)
You must be signed in to star a gist -
Fork
(33)
You must be signed in to fork a gist
-
-
Save Mo45/cb0813cb8a6ebcd6524f6a36d4f8862c to your computer and use it in GitHub Desktop.
PHP - Send message to Discord via Webhook
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 | |
//======================================================================================================= | |
// Create new webhook in your Discord channel settings and copy&paste URL | |
//======================================================================================================= | |
$webhookurl = "YOUR_WEBHOOK_URL"; | |
//======================================================================================================= | |
// Compose message. You can use Markdown | |
// Message Formatting -- https://discordapp.com/developers/docs/reference#message-formatting | |
//======================================================================================================== | |
$timestamp = date("c", strtotime("now")); | |
$json_data = json_encode([ | |
// Message | |
"content" => "Hello World! This is message line ;) And here is the mention, use userID <@12341234123412341>", | |
// Username | |
"username" => "krasin.space", | |
// Avatar URL. | |
// Uncoment to replace image set in webhook | |
//"avatar_url" => "https://ru.gravatar.com/userimage/28503754/1168e2bddca84fec2a63addb348c571d.jpg?size=512", | |
// Text-to-speech | |
"tts" => false, | |
// File upload | |
// "file" => "", | |
// Embeds Array | |
"embeds" => [ | |
[ | |
// Embed Title | |
"title" => "PHP - Send message to Discord (embeds) via Webhook", | |
// Embed Type | |
"type" => "rich", | |
// Embed Description | |
"description" => "Description will be here, someday, you can mention users here also by calling userID <@12341234123412341>", | |
// URL of title link | |
"url" => "https://gist.github.com/Mo45/cb0813cb8a6ebcd6524f6a36d4f8862c", | |
// Timestamp of embed must be formatted as ISO8601 | |
"timestamp" => $timestamp, | |
// Embed left border color in HEX | |
"color" => hexdec( "3366ff" ), | |
// Footer | |
"footer" => [ | |
"text" => "GitHub.com/Mo45", | |
"icon_url" => "https://ru.gravatar.com/userimage/28503754/1168e2bddca84fec2a63addb348c571d.jpg?size=375" | |
], | |
// Image to send | |
"image" => [ | |
"url" => "https://ru.gravatar.com/userimage/28503754/1168e2bddca84fec2a63addb348c571d.jpg?size=600" | |
], | |
// Thumbnail | |
//"thumbnail" => [ | |
// "url" => "https://ru.gravatar.com/userimage/28503754/1168e2bddca84fec2a63addb348c571d.jpg?size=400" | |
//], | |
// Author | |
"author" => [ | |
"name" => "krasin.space", | |
"url" => "https://krasin.space/" | |
], | |
// Additional Fields array | |
"fields" => [ | |
// Field 1 | |
[ | |
"name" => "Field #1 Name", | |
"value" => "Field #1 Value", | |
"inline" => false | |
], | |
// Field 2 | |
[ | |
"name" => "Field #2 Name", | |
"value" => "Field #2 Value", | |
"inline" => true | |
] | |
// Etc.. | |
] | |
] | |
] | |
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); | |
$ch = curl_init( $webhookurl ); | |
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-type: application/json')); | |
curl_setopt( $ch, CURLOPT_POST, 1); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $json_data); | |
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt( $ch, CURLOPT_HEADER, 0); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1); | |
$response = curl_exec( $ch ); | |
// If you need to debug, or find out why you can't send message uncomment line below, and execute script. | |
// echo $response; | |
curl_close( $ch ); | |
I just created a package for this. https://packagist.org/packages/atakde/discord-webhook-php https://github.com/atakde/discord-webhook-php
How do you do embeds? Your documentation does not cover them.
You are right, actually we don't have documentation. I will prepare :)
You can use like that;$messageFactory = new MessageFactory(); $message= $messageFactory->create('embed');
https://github.com/atakde/discord-webhook-php/blob/master/src/Message/EmbedMessage.php
I tried to use it with fields and it just wouldn't work. I ended up going with my own solution.
$embedMessage = $messageFactory->create('embed');
$embedMessage->setTitle("Title");
$embedMessage->setAuthorUrl("https://doodleipsum.com/700?i=f8b1abea359b643310916a38aa0b0562");
$embedMessage->setAuthorIcon("https://doodleipsum.com/700?i=f8b1abea359b643310916a38aa0b0562");
$embedMessage->setFields([
[
'name' => 'Field 1',
'value' => 'Value 1',
'inline' => true
],
[
'name' => 'Field 2',
'value' => 'Value 2',
'inline' => false
]
]);
$webhook = new DiscordWebhook($embedMessage);
$webhook->setWebhookUrl("YOUR_WEBHOOK_URL");;
$res = $webhook->send();
I tested in my end and it is working. Feel free to mail me the issue. This is the example that I tested.
works well thanks <3
can confirm this works , thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I tried to use it with fields and it just wouldn't work. I ended up going with my own solution.