Download the PHP package mhhidayat/php-discord-client without Composer
On this page you can find all versions of the php package mhhidayat/php-discord-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mhhidayat/php-discord-client
More information about mhhidayat/php-discord-client
Files in mhhidayat/php-discord-client
Package php-discord-client
Short Description Lightweight PHP client for Discord Webhook and Bot API
License MIT
Informations about the package php-discord-client
PHP Discord Client
A comprehensive PHP library for interacting with Discord via both webhooks and bot API. Send messages, rich embeds, and manage Discord communications with an elegant, fluent interface.
Features
- Webhook Support: Send messages via Discord webhooks
- Bot API Support: Send messages using Discord bot tokens
- Rich Embeds: Create beautiful embeds with the fluent builder
- Flexible Content: Support for text, embeds, and custom payloads
- Error Handling: Comprehensive validation and error reporting
- Modern PHP: Built for PHP 8.0+ with type safety and modern features
Requirements
- PHP 8.0 or higher
- cURL extension enabled
- Composer
Dependencies
This library includes:
vlucas/phpdotenvfor environment variable management (useful for storing tokens and webhook URLs securely)
Installation
Install via Composer:
Quick Start
Basic Usage
Webhook Messages
Send messages using Discord webhooks:
Bot Messages
Send messages using a Discord bot token:
Rich Embeds with Builder
Use the fluent embed builder for creating rich embeds with both webhooks and bots:
Custom Content with Raw Arrays
You can use raw arrays for custom content with both webhooks and bots:
Customize Username and Avatar
Customize the appearance of your messages (webhooks support username/avatar override, bots use their configured identity):
Text-to-Speech (TTS)
Enable TTS for your messages with both webhooks and bots:
Webhooks vs Bots
When to Use Webhooks
Webhooks are perfect for:
- Simple notifications and alerts
- External integrations (CI/CD, monitoring systems)
- One-way communication to Discord
- When you don't need a persistent bot presence
- Quick setup without bot permissions
When to Use Bots
Bots are ideal for:
- Interactive applications that need to respond to users
- Complex Discord integrations
- When you need advanced Discord features
- Applications requiring persistent presence
- Better rate limiting and API access
Setup Requirements
Webhooks:
- Create a webhook in your Discord server settings
- Copy the webhook URL
- Start sending messages immediately
Bots:
- Create a bot application in Discord Developer Portal
- Generate and copy the bot token
- Invite the bot to your server with appropriate permissions
- Get the channel ID where you want to send messages
- Use the bot token and channel ID in your code
Advanced Usage
Advanced Embed Fields with Builder
For more complex field management, you can use the EmbedsFieldsContract:
Video Embeds
Include video content in your embeds:
Embed Builder Features
The embed builder provides a fluent interface for creating rich embeds:
Available Colors Enum
The library includes a comprehensive Colors enum with Discord's official colors:
Conditional Sending
Send messages only when a condition is met:
Custom Headers
Custom Timeout
Default timeout is 15 seconds. You can customize it:
Dynamic Content with Closures
Check Response Status
Security Best Practices
Environment Variables
Store sensitive information like bot tokens and webhook URLs in environment variables:
Token Security
- Never commit tokens or webhook URLs to version control
- Use environment variables or secure configuration management
- Rotate tokens regularly
- Limit bot permissions to only what's needed
- Monitor bot usage and API calls
API Reference
Common Methods (Both DiscordWebhook and DiscordBot)
make(): self
Create a new instance of the Discord client.
text(string $text): self
Set a simple text message (max 2000 characters).
setContent(array|Closure $content): self
Set custom content including embeds. Accepts an array or closure that returns an array.
addEmbeds(Closure $embedsHandler): self
Add rich embeds using the fluent EmbedsContract builder. The closure receives an EmbedsContract instance.
allowTTS(): self
Enable text-to-speech for the message.
send(): self
Send the message to Discord.
sendWhen(bool|Closure $condition): self
Send the message only if the condition is true.
successful(): bool
Check if the last request was successful (HTTP 2xx).
failed(): bool
Check if the last request failed.
getResponseJson(): string
Get the raw JSON response from Discord.
Webhook-Specific Methods (DiscordWebhook)
setWebhookURL(string $url): self
Set the Discord webhook URL (required for webhooks).
setUsername(string $username): self
Override the default webhook username.
setAvatar(string $avatarURL): self
Override the default webhook avatar.
Bot-Specific Methods (DiscordBot)
setBotToken(string $token): self
Set the Discord bot token (required for bots).
setChannelID(string $channelID): self
Set the target channel ID where the bot will send messages (required for bots).
Static Methods (Both Classes)
withHeaders(array $headers): self
Create instance with custom HTTP headers.
timeout(int $seconds): self
Create instance with custom timeout (default: 15 seconds).
withConfig(array $config): self
Create instance with configuration array.
Embed Builder Methods
The EmbedsContract provides these methods for building rich embeds:
title(string $title): self
Set the embed title.
description(string $description): self
Set the embed description.
url(string $url): self
Set the embed URL (makes the title clickable).
color(Colors|int $color): self
Set the embed color using the Colors enum or a custom integer.
enableTimestamp(): self
Add the current timestamp to the embed.
authorName(string $authorName): self
Set the author name.
authorUrl(string $authorUrl): self
Set the author URL (makes the author name clickable).
authorIconUrl(string $authorIconUrl): self
Set the author icon URL.
footerText(string $footerText): self
Set the footer text.
footerIconUrl(string $footerIconUrl): self
Set the footer icon URL.
imageUrl(string $imageUrl): self
Set the embed image URL (must be HTTPS).
imageWidth(int $width): self
Set the embed image width in pixels.
imageHeight(int $height): self
Set the embed image height in pixels.
thumbnailUrl(string $thumbnailUrl): self
Set the embed thumbnail URL.
thumbnailWidth(int $width): self
Set the embed thumbnail width in pixels.
thumbnailHeight(int $height): self
Set the embed thumbnail height in pixels.
providerName(string $providerName): self
Set the embed provider name.
providerUrl(string $providerUrl): self
Set the embed provider URL.
Embed Fields Builder Methods
The EmbedsFieldsContract provides these methods for building embed fields:
name(string $name): self
Set the field name. Must be called before value() and inline().
value(string $value): self
Set the field value. Must be called after name().
inline(bool $inline): self
Set whether the field should be displayed inline. Must be called after name().
build(): array
Build and return the fields array (used internally).
fields(array|Closure $fields): self
Add fields to the embed (max 25 fields). Each field should have 'name', 'value', and optionally 'inline' keys. Accepts either an array or a closure with EmbedsFieldsContract for more control.
videoUrl(string $videoUrl): self
Set the embed video URL.
videoWidth(int $width): self
Set the embed video width in pixels.
videoHeight(int $height): self
Set the embed video height in pixels.
Error Handling
The library throws DiscordClientException for validation errors:
Common validation errors:
- Text exceeding 2000 characters
- More than 25 fields in an embed
- Missing webhook URL (for webhooks)
- Missing bot token or channel ID (for bots)
- Missing content (no text or setContent called)
- Image URLs that don't use HTTPS protocol
- Embed title exceeding 256 characters
- Embed description exceeding 4096 characters
Examples
Notification System
Error Logging
Deployment Notifications
Rich Media Embeds
Showcase images and thumbnails in your embeds:
Product Showcase
Perfect for e-commerce or product announcements:
Troubleshooting
Common Issues
"Webhook URL is not set" Error
"No content is set" Error
Bot Messages Not Sending
- Verify bot token is correct
- Ensure bot has "Send Messages" permission in the target channel
- Check that channel ID is correct
- Confirm bot is added to the server
Webhook Messages Not Appearing
- Verify webhook URL is correct and active
- Check webhook hasn't been deleted from Discord
- Ensure webhook has permission to post in the channel
Debug Response
Check the response from Discord for debugging:
License
See LICENSE for details.
Author
mhhidayat - [email protected]