PHP code example of almighty-shogun / discord-webhook

1. Go to this page and download the library: Download almighty-shogun/discord-webhook library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

almighty-shogun / discord-webhook example snippets




use DiscordWebhook\DiscordWebhook;
use DiscordWebhook\Error\DiscordWebhookException;
use DiscordWebhook\Messages\{TextMessage, DiscordEmbed};

$discordWebhook = new DiscordWebhook("https://discord.com/api/webhooks/xxx/xxxx");

// Text message
$message = new TextMessage();
$embed = new DiscordEmbed();

$embed->setTitle("Test Embed")
    ->setDescription("This is a test embed.")
    ->setColor("#228b22") // Hex color code without the # symbol.
    ->setURL("https://github.com/Almighty-Shogun")
    ->setFooter("Test Footer", "https://avatars.githubusercontent.com/u/96011415?v=4")
    ->setTimestamp()
    ->setThumbnail("https://avatars.githubusercontent.com/u/96011415?v=4")
    ->setImage("https://avatars.githubusercontent.com/u/96011415?v=4")
    ->setAuthor("Almighty Shogun", "https://avatars.githubusercontent.com/u/96011415?v=4")
    ->addField("Field 1", "This is a test field.")
    ->addField("Field 2", "This is another test field.")
    ->addField("Field 3", "This is a third test field.");

$textMessage->setUsername("Almighty Shogun")
    ->setAvatar("https://avatars.githubusercontent.com/u/96011415?v=4")
    ->setContent("This is a test message.");

try {
    $discordWebhook->send($textMessage);
} catch (DiscordWebhookException $e) {
    echo $e->getMessage();
}



$discordWebhook = discordWebhook("https://discord.com/api/webhooks/xxx/xxxx");

$embed = discordEmbed()->setTitle("Test Embed")
    ->setDescription("This is a test embed.")
    ->setColor("228b22") // Hex color code without the # symbol.
    ->setURL("https://github.com/Almighty-Shogun")
    ->setFooter("Test Footer", "https://avatars.githubusercontent.com/u/96011415?v=4")
    ->setTimestamp()
    ->setThumbnail("https://avatars.githubusercontent.com/u/96011415?v=4")
    ->setImage("https://avatars.githubusercontent.com/u/96011415?v=4")
    ->setAuthor("Almighty Shogun", "https://avatars.githubusercontent.com/u/96011415?v=4")
    ->addField("Field 1", "This is a test field.")
    ->addField("Field 2", "This is another test field.")
    ->addField("Field 3", "This is a third test field.");

$textMessage = discordMessage()
    ->setUsername("Almighty Shogun")
    ->setAvatar("https://avatars.githubusercontent.com/u/96011415?v=4")
    ->setContent("This is a test message.")
    ->addEmbed($embed);

try {
    $discordWebhook->send($textMessage);
} catch (DiscordWebhookException $e) {
    echo $e->getMessage();
}