PHP code example of justinkluever / discord-webhook-builder

1. Go to this page and download the library: Download justinkluever/discord-webhook-builder 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/ */

    

justinkluever / discord-webhook-builder example snippets


use JustinKluever\DiscordWebhookBuilder\Components\TextDisplay;
use JustinKluever\DiscordWebhookBuilder\Enums\Support\MessageFlag;
use JustinKluever\DiscordWebhookBuilder\Support\Webhook\Embed;
use JustinKluever\DiscordWebhookBuilder\Webhook;

// Components V2
$webhook = Webhook::make()
    ->flag(MessageFlag::IS_COMPONENTS_V2) // Required for Components V2 by Discord
    ->component(
        TextDisplay::make('This is a Text Display!'),
    );

// Classic Webhooks
$webhook = Webhook::make()
    ->content('Embed Test webhook')
    ->embed(
        Embed::make()->description('This is a Embed description'),
    )

// Laravel HTTP Client
use Illuminate\Support\Facades\Http;
Http::post('https://discord.com/api/webhooks/...', $webhook); // The Webhook builder implements Stringable for ease of use

// Guzzle
$client->post('https://discord.com/api/webhooks/...', [
    'json' => $webhook
]);

// cURL
$ch = curl_init('https://discord.com/api/webhooks/...');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $webhook);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);