Skip to content

Instantly share code, notes, and snippets.

@Mo45
Last active April 15, 2024 19:53
Show Gist options
  • Save Mo45/cb0813cb8a6ebcd6524f6a36d4f8862c to your computer and use it in GitHub Desktop.
Save Mo45/cb0813cb8a6ebcd6524f6a36d4f8862c to your computer and use it in GitHub Desktop.
PHP - Send message to Discord via Webhook
<?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 );
@debug420
Copy link

@debug420, your code is wrong. Learn how to execute and debug. Also Discord docs.
Here is fully working code.

Hmm still doesnt seem to work. And I apologize, I don't use php and I'm just trying to get this to work so I can move on.
I tried hosting it too on Awardspace.com but with no luck. Any clue as to why this is occuring?
(I copied the entire code you sent and just replaced the url with my webhook)

@strawbberrys
Copy link

Can you not send discord webhook requests with 000webhost? I wrote my own code and it didn't work so I came to this and this doesn't work either. It just keeps returning the webhook data you would get with a GET request, but it's not sending any data.

@ArMaaa
Copy link

ArMaaa commented Mar 17, 2021

Just to clarify, this code works as it should work.
I'm only wondering is it possible to send reactions with this?

@Mo45
Copy link
Author

Mo45 commented Mar 18, 2021

Just to clarify, this code works as it should work.
I'm only wondering is it possible to send reactions with this?

Discord documentation.

@neovorg
Copy link

neovorg commented Apr 12, 2021

Works flawlessly. Thanks a lot!

@nschwaller
Copy link

nschwaller commented Apr 15, 2021

Hey i have a problem, my code run in linux but not in my IIS website.
when i display $response on my IIS server it returns false. I don't know why. My two servers are in https.
EDIT: I have var_dump($json_date) is everything is fine I do not understand
Thanks

@Mo45
Copy link
Author

Mo45 commented Apr 15, 2021

@nschwaller most likely you need to install curl extension. Read your server error logs.

@nschwaller
Copy link

nschwaller commented Apr 15, 2021

i have curl in my iis server...
2021-04-15 12:19:51 152.228.218.73 GET /neverland/ - 443 - (my ip serv) Mozilla/5.0+(compatible;+Discordbot/2.0;++https://discordapp.com/) - 200 0 0 93

edit: mb this my error :
[15-Apr-2021 13:00:57 UTC] PHP Warning: PHP Startup: Unable to load dynamic library 'php_xmlrpc.dll' (tried: C:\Program Files\PHP\v8.0\ext\php_xmlrpc.dll (The specified module could not be found), C:\Program Files\PHP\v8.0\ext\php_php_xmlrpc.dll.dll (The specified module could not be found)) in Unknown on line 0
image

@trentwiles
Copy link

@FreeStyleUK
Copy link

Works brilliantly. Is it possible to send a direct message to a user using a variant of this? (Not mentioning a user, but actually private messaging them)

@trentwiles
Copy link

You would need to use a discord bot to do that. (see discord.php)

@FreeStyleUK
Copy link

You would need to use a discord bot to do that. (see discord.php)

Apologies if it is blatantly obvious, but where is the discord.php?

@olliedean
Copy link

You would need to use a discord bot to do that. (see discord.php)

Apologies if it is blatantly obvious, but where is the discord.php?

https://github.com/discord-php/DiscordPHP

@verone0001
Copy link

@Mo45 thank you for this, it works flawlessly! Although I couldn't make it work for .svg images, any clue?

@trentwiles
Copy link

SVG images don't render on discord

@MasterSix997
Copy link

how can i edit or delete a message sent by webhook?

@Mo45
Copy link
Author

Mo45 commented Oct 8, 2021

how can i edit or delete a message sent by webhook?

Hello. This is isn't Discord API support forum =)
1min Googling give me this thread, maybe it will help.

@BrosweynoforFire
Copy link

This code is a 1 in a million jackpot its work insane but I'm trying to learn how to add it to my form submit button can you help me by any chance, please. thanks

@younes-dro
Copy link

@Mo45
thank you for this code.
i want ask , if it's possible to send a PDF as attachment ?

@trentwiles
Copy link

Yep, Discord accepts most if not all file types.

@JmilamIF
Copy link

JmilamIF commented May 11, 2022

@BrosweynoforFire

Did you ever figure out how to make it work on submit?

@younes-dro
Copy link

@BrosweynoforFire

Did you ever figure out how to make it work on submit?
you can using this endpoint API : /channels/ {channel_id {/messages}

@atakde
Copy link

atakde commented Sep 10, 2022

@ThatProgrammerr
Copy link

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.

@atakde
Copy link

atakde commented Sep 22, 2022

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

@ThatProgrammerr
Copy link

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.

@atakde
Copy link

atakde commented Sep 22, 2022

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.

@ardian-syla
Copy link

works well thanks <3

@CPHgroup
Copy link

can confirm this works , thanks

@BurkenDev
Copy link

@debug420, your code is wrong. Learn how to execute and debug. Also Discord docs. Here is fully working code.

Stop being negative in the comments thank you!

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