Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an attachment/file via discord webhook

I have been trying to figure out how to send an attachment in webhooks. I've been trying hours but cannot figure it out. I've been reading the this but I still haven't had much link. I've also been reading the docs but struggling to understand. Can anyone assist me?

<pre><?php 

$url = "WEBHOOK URL HERE";

$headers = [ 'Content-Type: multipart/form-data; charset=utf-8' ];
$POST = [
    // Message
    "content" => "Hello World!",
    
    // Username
    "username" => "testuser",

    // File upload
    "file" => curl_file_create("image.gif", 'image/gif', 'image')

];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($POST));
$response   = curl_exec($ch);
var_dump($response);

I'm getting the error

"{"message": "Cannot send an empty message", "code": 50006}"
like image 718
Zoey Malkov Avatar asked Feb 02 '26 10:02

Zoey Malkov


1 Answers

Replace

"file" => curl_file_create("image.gif", 'image/gif', 'image.gif')

With

"file" => curl_file_create(realpath("image.gif"), 'image/gif', 'image.gif')
<pre><?php 

$url = "WEBHOOK URL HERE";

$headers = [ 'Content-Type: multipart/form-data; charset=utf-8' ];
$POST = [
    // Message
    "content" => "Hello World!",
    
    // Username
    "username" => "testuser",

    // File upload
    "file" => curl_file_create(realpath("image.gif"), 'image/gif', 'image.gif')

];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($POST));
$response   = curl_exec($ch);
var_dump($response);

Above code is only upload your own server file to discord.

If you want to get file from other URL and upload to Discord then you need to upload same file first on your own server as temp and then set this temp file here..

"file" => curl_file_create(realpath($tempfile), 'image/gif', 'image.gif')

after the

$response   = curl_exec($ch);
unlink($tempfile); //delete temp file if you want.
like image 193
Surya Bhai Avatar answered Feb 03 '26 23:02

Surya Bhai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!