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
//========================================================================================================
$msg = "Test **message** [https://krasin.space(<URL>)";
$json_data = array ('content'=>"$msg");
$make_json = json_encode($json_data);
$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, $make_json);
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;
?>
@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!

@kozlovcode

This comment has been minimized.

Copy link

kozlovcode commented Feb 22, 2019

Thanks!

@Tortonas

This comment has been minimized.

Copy link

Tortonas 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>

@Tortonas

This comment has been minimized.

Copy link

Tortonas 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.

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.