PHP code example of mhhidayat / php-discord-client
1. Go to this page and download the library: Download mhhidayat/php-discord-client 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/ */
mhhidayat / php-discord-client example snippets
use Mhhidayat\PhpDiscordClient\DiscordWebhook;
use Mhhidayat\PhpDiscordClient\DiscordBot;
// Send via webhook (simple setup)
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Hello from webhook!')
->send();
// Send via bot (more features)
DiscordBot::make()
->setBotToken('YOUR_BOT_TOKEN')
->setChannelID('YOUR_CHANNEL_ID')
->text('Hello from bot!')
->send();
use Mhhidayat\PhpDiscordClient\DiscordWebhook;
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Hello, Discord!')
->send();
use Mhhidayat\PhpDiscordClient\DiscordBot;
DiscordBot::make()
->setBotToken('YOUR_BOT_TOKEN')
->setChannelID('YOUR_CHANNEL_ID')
->text('Hello from bot!')
->send();
use Mhhidayat\PhpDiscordClient\DiscordWebhook;
use Mhhidayat\PhpDiscordClient\DiscordBot;
use Mhhidayat\PhpDiscordClient\Contract\EmbedsContract;
use Mhhidayat\PhpDiscordClient\Enums\Colors;
// Using webhook
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('Check out this embed!')
->addEmbeds(function (EmbedsContract $embed) {
$embed->title('Embed Title')
->description('This is an embed description')
->color(Colors::Blue)
->url('https://example.com')
->enableTimestamp()
->fields([
[
'name' => 'Field 1',
'value' => 'Value 1',
'inline' => true
],
[
'name' => 'Field 2',
'value' => 'Value 2',
'inline' => true
]
]);
})
->send();
// Using bot (same embed builder interface)
DiscordBot::make()
->setBotToken('YOUR_BOT_TOKEN')
->setChannelID('YOUR_CHANNEL_ID')
->addEmbeds(function (EmbedsContract $embed) {
$embed->title('Bot Embed')
->description('Sent via bot API')
->color(Colors::Green);
})
->send();
// Webhook with custom appearance
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->setUsername('Custom Bot Name')
->setAvatar('https://example.com/avatar.png')
->text('Message with custom appearance')
->send();
// Bot messages use the bot's configured username and avatar
DiscordBot::make()
->setBotToken('YOUR_BOT_TOKEN')
->setChannelID('YOUR_CHANNEL_ID')
->text('Message from bot')
->send();
// Webhook with TTS
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->text('This message will be read aloud')
->allowTTS()
->send();
// Bot with TTS
DiscordBot::make()
->setBotToken('YOUR_BOT_TOKEN')
->setChannelID('YOUR_CHANNEL_ID')
->text('Bot TTS message')
->allowTTS()
->send();
// Webhook example - great for CI/CD notifications
DiscordWebhook::make()
->setWebhookURL($_ENV['DISCORD_WEBHOOK_URL'])
->setUsername('Deploy Bot')
->text('â Deployment to production completed successfully!')
->send();
// Bot example - better for interactive features
DiscordBot::make()
->setBotToken($_ENV['DISCORD_BOT_TOKEN'])
->setChannelID($_ENV['DISCORD_CHANNEL_ID'])
->text('đ¤ Bot is online and ready to help!')
->send();
use Mhhidayat\PhpDiscordClient\DiscordWebhook;
use Mhhidayat\PhpDiscordClient\Contract\EmbedsContract;
use Mhhidayat\PhpDiscordClient\Contract\EmbedsFieldsContract;
use Mhhidayat\PhpDiscordClient\Enums\Colors;
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->addEmbeds(function (EmbedsContract $embed) {
$embed->title('Server Statistics')
->description('Current server performance metrics')
->color(Colors::Blue)
->fields(function (EmbedsFieldsContract $fields) {
$fields->name('CPU Usage')->value('45%')->inline(true);
$fields->name('Memory')->value('2.1GB / 8GB')->inline(true);
$fields->name('Disk Space')->value('120GB / 500GB')->inline(true);
$fields->name('Network')->value('1.2 Mbps')->inline(true);
$fields->name('Uptime')->value('15 days, 4 hours')->inline(true);
$fields->name('Status')->value('â All systems operational')->inline(false);
});
})
->send();
DiscordWebhook::make()
->setWebhookURL('https://discord.com/api/webhooks/YOUR_WEBHOOK_URL')
->addEmbeds(function (EmbedsContract $embed) {
$embed->title('Tutorial Video')
->description('Learn how to use our new feature')
->color(Colors::Purple)
->videoUrl('https://example.com/tutorial.mp4')
->videoWidth(1280)
->videoHeight(720)
->thumbnailUrl('https://example.com/video-thumbnail.jpg');
})
->send();