Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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 );
@hungaryck

This comment has been minimized.

Copy link

hungaryck commented Oct 6, 2018

Thanks! :)

@karinnahmay

This comment has been minimized.

Copy link

karinnahmay commented Jan 31, 2019

Hi, tried using this style but when i print $response its empty. How can I see the response to my curl request to see errors. Thanks!

@roakozlov

This comment has been minimized.

Copy link

roakozlov commented Feb 22, 2019

Thanks!

@kasteckis

This comment has been minimized.

Copy link

kasteckis commented Nov 12, 2019

@Mo45 Any way to trigger @everyone ? :D

@Mo45

This comment has been minimized.

Copy link
Owner Author

Mo45 commented Nov 13, 2019

@Mo45 Any way to trigger @everyone ? :D

Hello. I'm not sure but try this.

\@everyone or <@&everyone>

@kasteckis

This comment has been minimized.

Copy link

kasteckis commented Nov 15, 2019

My bad, it works with the regular @everyone :)

In order for a webhook bot to tag everyone, discord server must enable ability to use @everyone tag for everyone.

@Mo45

This comment has been minimized.

Copy link
Owner Author

Mo45 commented Nov 23, 2019

If you can't send message and get this error: {"message": "Cannot send an empty message", "code": 50006}
After this line $ch = curl_init( $webhookurl );
You need to add new one:
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));

@SapozhnikUA

This comment has been minimized.

Copy link

SapozhnikUA commented Nov 30, 2019

Thanks. My webhook stopped yesterday but your code helped me to solve problem.

@piotr-placzek

This comment has been minimized.

Copy link

piotr-placzek commented Mar 1, 2020

Thanks! :)

@lowe493

This comment has been minimized.

Copy link

lowe493 commented Mar 2, 2020

@Mo45 do you know if this would support posting an image along with the message?

@Mo45

This comment has been minimized.

Copy link
Owner Author

Mo45 commented Mar 3, 2020

@Mo45 do you know if this would support posting an image along with the message?

Read about Embed Objects.

@Mo45

This comment has been minimized.

Copy link
Owner Author

Mo45 commented Mar 16, 2020

@lowe493 do you know if this would support posting an image along with the message?

Updated code. Now with Embed Object.

@idadean911

This comment has been minimized.

Copy link

idadean911 commented Mar 19, 2020

question, what if i want to have a contact form and whatever you type in the boxes are posted on the corresponding boxes on the webhook

@Mo45

This comment has been minimized.

Copy link
Owner Author

Mo45 commented Mar 20, 2020

question, what if i want to have a contact form and whatever you type in the boxes are posted on the corresponding boxes on the webhook

You can call webhook on form submit, and send input data in additional fields array here:
"fields" => [ // Field 1 [ "name" => "Name", "value" => "$name", "inline" => false ], // Field 2 [ "name" => "email", "value" => "$email", "inline" => true ], // Field 3 [ "name" => "text", "value" => "$text", "inline" => true ] ]

@ferdisahin

This comment has been minimized.

Copy link

ferdisahin commented Mar 28, 2020

Wow, thank you :)

@ProfessorVarox

This comment has been minimized.

Copy link

ProfessorVarox commented Mar 30, 2020

Hello, I always get this error: {"message": "400: Bad Request", "code": 0}

Any ideas how to fix it?

@Mo45

This comment has been minimized.

Copy link
Owner Author

Mo45 commented Mar 31, 2020

Hello, I always get this error: {"message": "400: Bad Request", "code": 0}

Any ideas how to fix it?

@ProfessorVarox Are you get your webhook URL and changed this value to your URL?
$webhookurl = "YOUR_WEBHOOK_URL";

@Jimjimkewl

This comment has been minimized.

Copy link

Jimjimkewl commented Apr 8, 2020

Hi, can you make a sample on how to mention a user with webhook and php?

@Mo45

This comment has been minimized.

Copy link
Owner Author

Mo45 commented Apr 9, 2020

Hi, can you make a sample on how to mention a user with webhook and php?

Updated code. Add <@12341234123412341> with userID into content or description.
You can find your userID here.
It was alredy answered almost year ago.

@NerofleLincoln

This comment has been minimized.

Copy link

NerofleLincoln commented Apr 21, 2020

Hi, your script is so useful, thank you !
But what should I put in "file" to upload a file ?

@Mo45

This comment has been minimized.

Copy link
Owner Author

Mo45 commented Apr 22, 2020

But what should I put in "file" to upload a file ?

You should put FILE. Read docs, please.

@NerofleLincoln

This comment has been minimized.

Copy link

NerofleLincoln commented Apr 22, 2020

I don't know what I'm doing wrong.

$json_data = json_encode([
"payload_json" => [
"content" => "content",
],
"file" => "https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/langfr-280px-Google_2015_logo.svg.png",
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);

$ch = curl_init($webhookurl);
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array('Content-type: multipart/form-data','Content-Disposition: form-data; name="file"; filename="filename.png"')
);
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 get {"message": "Cannot send an empty message", "code": 50006}. I googled but couldn't find an answer.

[EDIT] : I finally got it working

@kishan-bot

This comment has been minimized.

Copy link

kishan-bot commented Apr 25, 2020

I want to make form for this
My form page index.php [ form action="send.php"]
Send.Php contain this above code I used post method

Now what should I do so it again redirected back to index.Php

@Mo45

This comment has been minimized.

Copy link
Owner Author

Mo45 commented Apr 25, 2020

Now what should I do so it again redirected back to index.Php

Read this.

@alystair

This comment has been minimized.

Copy link

alystair commented Apr 26, 2020

Great starting point, thanks for keeping me lazy 👍

@ml1969

This comment has been minimized.

Copy link

ml1969 commented Apr 30, 2020

Hi Mo45, great job thanks.
I was looking for sume stuff like this, to send message direct to Discord from a php script.
One additional question.
Can I insert/ send html code?
i have to send a table with results
Michael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.