PHP code example of renzojohnson / discord-api

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

    

renzojohnson / discord-api example snippets


use RenzoJohnson\Discord\Discord;

$discord = new Discord('your-bot-token');

// Send a text message to a channel
$discord->sendMessage('CHANNEL_ID', 'Hello from PHP!');

$discord->sendMessage('CHANNEL_ID', 'Hello World');

use RenzoJohnson\Discord\Message\Embed;

$embed = (new Embed())
    ->title('Server Alert')
    ->description('CPU usage is critical')
    ->color(0xFF0000)
    ->field('Server', 'web-01', inline: true)
    ->field('CPU', '95%', inline: true)
    ->footer('Monitoring Bot')
    ->timestamp(date('c'));

$discord->sendEmbed('CHANNEL_ID', $embed);

// Embed with text
$discord->sendEmbed('CHANNEL_ID', $embed, content: 'Attention!');

$discord->editMessage('CHANNEL_ID', 'MESSAGE_ID', 'Updated content');

$discord->deleteMessage('CHANNEL_ID', 'MESSAGE_ID');

$discord->addReaction('CHANNEL_ID', 'MESSAGE_ID', '👍');

$channel = $discord->getChannel('CHANNEL_ID');
$channels = $discord->getGuildChannels('GUILD_ID');

use RenzoJohnson\Discord\Webhook\Webhook;

$webhook = new Webhook('https://discord.com/api/webhooks/ID/TOKEN');

// Simple text
$webhook->send('Deploy complete!');

// Rich embed
use RenzoJohnson\Discord\Message\Embed;

$embed = (new Embed())
    ->title('Deployed v2.1.0')
    ->description('All systems operational')
    ->color(0x00FF00)
    ->footer('CI/CD Pipeline');

$webhook->sendEmbed($embed, content: 'Production deploy finished');

use RenzoJohnson\Discord\Message\WebhookMessage;

$message = new WebhookMessage(
    content: 'Alert!',
    username: 'AlertBot',
    avatarUrl: 'https://example.com/bot.png',
);

$embed = (new Embed())->title('Error')->description('Database connection failed')->color(0xFF0000);
$message->addEmbed($embed);

$webhook->send($message);

$embed = (new Embed())
    ->title('Title')
    ->description('Description text')
    ->url('https://example.com')
    ->color(0x5865F2)              // Discord blurple
    ->author('Bot Name', 'https://example.com', 'https://example.com/icon.png')
    ->thumbnail('https://example.com/thumb.png')
    ->image('https://example.com/image.png')
    ->field('Field 1', 'Value 1', inline: true)
    ->field('Field 2', 'Value 2', inline: true)
    ->footer('Footer text', 'https://example.com/footer-icon.png')
    ->timestamp(date('c'));

use RenzoJohnson\Discord\Webhook\InteractionVerifier;

// Verify signature and get body
$body = InteractionVerifier::verify('YOUR_PUBLIC_KEY');
$interaction = json_decode($body, true);

// Respond to PING
if ($interaction['type'] === 1) {
    InteractionVerifier::respondToPing();
}

use RenzoJohnson\Discord\Exception\AuthenticationException;
use RenzoJohnson\Discord\Exception\RateLimitException;
use RenzoJohnson\Discord\Exception\DiscordException;

try {
    $discord->sendMessage('CHANNEL_ID', 'Hello');
} catch (AuthenticationException $e) {
    // Invalid bot token (401)
} catch (RateLimitException $e) {
    $retryAfter = $e->getRetryAfter(); // seconds (float)
} catch (DiscordException $e) {
    // Other API errors
    $errorData = $e->getErrorData();
}