The Novice Guide to Connecting PHP to Discord Using Webhooks

Through my searching for a way to bridge Gaiscioch.com and Discord I found a serious void of information when it came to Webhooks and PHP. There are a few github resources available that handle the hard stuff like oauth2 authentication and a lot of the pieces you need to run a PHP Discord Bot. However for the novice web programmer who is just looking to post an event, news announcement, or something similar without the need for a full time bot. This is where Webhooks come in.

This is an easy way for entry level PHP programmers to connect their websites to discord to send one way messages to their discord channels. This short tutorial and code sample will allow you to get up and running in no time.

Creating a Webhook

To create a webhook simply:

  1. Open your Discord.
  2. Click on the Gear to the right of the text channel you wish to post to.
  3. Click on "Webhooks"
  4. Click "Create Webhook"
  5. You can name your webhook and give it a cool icon.
  6. Copy and paste your Webhook URL into the code below.

The PHP Script

The first part of this script is a function that will handle posting the data to your discord.

<?PHP
function discordmsg($msg, $webhook) {
  if($webhook != "") {
    $ch = curl_init($webhook);
    $msg = "payload_json=" . urlencode(json_encode($msg))."";
    
    if(isset($ch)) {
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
      curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
      curl_close($ch);
      return $result;
    }
  }
}
?>

The next part is the actual action and message you wish to send.

<?PHP
// URL FROM DISCORD WEBHOOK SETUP
$webhook = "https://discordapp.com/api/webhooks/xxxxxxxxx/xxxxxxxx"; 
$msg = json_decode('
{
    "username":"BOTNAME",
    "content":"The message the BOTNAME posts.",
    "embeds": [{
        "title":"The Link Title",
        "description":"The Link Description",
        "url":"https://www.thelinkurl.com/",
        "color":DECIMALCOLORCODE,
        "author":{
            "name":"Site Name",
            "url":"https://www.sitelink.com/",
            "icon_url":"URLTOIMG"
        },
        "fields":[
            {
                "name":"LISTITEM1",
                "value":"LISTVALUE1",
                "inline":true
            },
            {
                "name":"LISTITEM2",
                "value":"LISTVALUE2",
                "inline":true
            },
            {
                "name":"LISTITEM3",
                "value":"LISTVALUE3",
                "inline":true
            }]
    }]
}
', true);
 ?>

Lastly there's the call to fire the script.

<?PHP
discordmsg($msg, $webhook); // SENDS MESSAGE TO DISCORD
?>

Trial and Error

From here you can play with it. You can omit any part of it, other than the "username" and "content" fields. If you would like to deck out your message with cool colors be sure to visit: https://www.spycolor.com/ which will allow you to convert traditional web colors such as #002200 into decimal colors like 8704. Discord uses decimal colors so you'll need to convert those colors to make it work right. My advice is experiment a lot.

You can also find some additional features and formatting options at:  https://anidiotsguide.gitbooks.io/discord-js-bot-guide/examples/using-embeds-in-messages.html

Getting Fancy

There is one last tidbit that I feel you might enjoy. You can also add any Emoji that you have on your server directly into your message. This can be done by:

Enable Developer Mode:

  1. Click on the Gear in the bottom left next to your name
  2. Click on "appearance" under App Settings
  3. Scroll down to "Advanced" and toggle on "Developer Mode"

Creating an Emoji:

  1. Click the Emoji button to the right of the text dialog and choose the emoji you'd like to use in your script.
  2. You will see a piece of code in the text box like ":smile:". Take note of that, then press Return.
  3. Right click the emoji and click "Copy ID"
  4. Next fill the 2 pieces of information into the below tag.
<:Emoji:EmojiID>

Example:

<:smile:364504524896862220>

This can be done with any Emoji you've uploaded to your server. You can simply place that into any of the JSON fields containing text to make the Emoji appear.

Share This Page