Home

Awesome

Discord SDK for PHP

An ultra-lightweight PHP SDK for accessing the Discord API and Discord webhook endpoints. This SDK does not currently support the Discord Gateway (i.e. the WebSocket interface).

Donate Discord

Features

Getting Started (API)

Set up Discord API access:

Create a 30 minute, single use invite for temporary members:

<?php
	require_once "support/sdk_discord.php";

	// Replace the string with the bot token above.
	$bottoken = "YOUR_BOT_TOKEN_HERE";

	// Replace this with a valid channel ID.
	// The channel ID can be found at the end of the URL in Discord.
	$channelid = "YOUR_CHANNEL_ID_HERE";

	// Create a temporary invite.
	$discord = new DiscordSDK();
	$discord->SetAccessInfo("Bot", $bottoken);

	// For a complete list of options:
	// https://discord.com/developers/docs/resources/channel#create-channel-invite
	$options = array(
		"max_age" => 1800,
		"max_uses" => 1,
		"unique" => true,
		"temporary" => true
	);

	$result = $discord->RunAPI("POST", "channels/" . $channelid . "/invites", $options);
	if (!$result["success"])
	{
		var_dump($result);

		exit();
	}

	$url = "https://discord.gg/" . $result["data"]["code"];

	echo $url . "\n";
?>

Getting Started (Webhook)

Set up a webhook in Discord:

Send a webhook notification that posts a message in a channel:

<?php
	require_once "support/sdk_discord.php";

	// Replace the string with the Webhook URL above.
	$url = "URL_OF_WEBHOOK";

	// For a complete list of options:
	// https://discord.com/developers/docs/resources/webhook#execute-webhook
	$options = array(
		"content" => "It works!"
	);

	$result = DiscordSDK::SendWebhookMessage($url, $options);
	if (!$result["success"])
	{
		var_dump($result);

		exit();
	}
?>

Send a webhook notification that posts a message with a file attachment in a channel:

<?php
	require_once "support/sdk_discord.php";

	// Replace the string with the Webhook URL above.
	$url = "URL_OF_WEBHOOK";

	// For a complete list of options:
	// https://discord.com/developers/docs/resources/webhook#execute-webhook
	$options = array(
		"content" => "It works!"
	);

	// Attaching a file.
	$fileinfo = array(
		"name" => "file",
		"filename" => "mycat.jpg",
		"type" => "image/jpeg",
		"data" => file_get_contents("/path/to/mycat.jpg")
	);

	// OR attach multiple files.
//	$fileinfo = array(
//		array(
//			"name" => "file",
//			"filename" => "mycat.jpg",
//			"type" => "image/jpeg",
//			"data" => file_get_contents("/path/to/mycat.jpg")
//		),
//		array(
//			"name" => "file2",
//			"filename" => "othercat.jpg",
//			"type" => "image/jpeg",
//			"data" => file_get_contents("/path/to/othercat.jpg")
//		),
//	);

	$result = DiscordSDK::SendWebhookMessage($url, $options, $fileinfo);
	if (!$result["success"])
	{
		var_dump($result);

		exit();
	}
?>

More Information