Send Image along with text to Discord Using Webhooks

Asked

Viewed 2,293 times

2

I’m trying to make a script that sends news, updates, to a server Discord, for this I need to use the Webhooks. I’ve already set up the whole system, including already working.

But the system I made just sends text, and I would like to do this system to send also along with the text, two images, to get a nice layout for the news.

In the PHP I am using the following code to send messages to Discord:

<?php
function postToDiscord($message) {
    $data = array("content" => $message, "username" => "News");
    $curl = curl_init("https://discordapp.com/api/webhooks/2225546.......");
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    return curl_exec($curl);
}
?>

Only this way only sends text. Staying so there in Discord:

inserir a descrição da imagem aqui

But I wanted the news to come out like this:

inserir a descrição da imagem aqui

Can you handle me? I want to send these images from the top and the footer along with the text, so that the news appear with a more beautiful look.


For you to better understand how the system works Add the Test Server you create on Discord and the website that sends the news to Discord:

Discord: https://discord.gg/6nGdjnz Posting site: http://diegodevphp.esy.es/

1 answer

1

There are several ways to do what you want using CURL, one of her is:

$data = array("content" => $message, "username" => "News", "file" => "ENDERECO_DA_IMAGEM/IMAGEM.jpeg");
// Se sua versão do PHP for igual ou acima de 5.6.0
// a linha abaixo é necessária
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.