Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

997
Views
Using Discord API & cURL to send a Discord DM

How could I send a Discord DM with cURL? I've got it working w/ channel messages but a Discord DM is quite important to my Website to keep users updated. Below is what I've got so far, with the ID being a Discord User ID.

$url = 'https://discordapp.com/api/channels/591765736003731487/messages';

$ch = curl_init();
$f = fopen('request.txt', 'w');
curl_setopt_array($ch, array(
    CURLOPT_URL            => $url, 
    CURLOPT_HTTPHEADER     => array('Authorization : Bot <TOKEN>'), 
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_VERBOSE        => 1,
    CURLOPT_SSL_VERIFYPEER => 0,
    CURLOPT_STDERR         => $f,
));
$response = curl_exec($ch);
fclose($f);
curl_close($ch);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Using your current code, i've made a small snippet. You might need to change a few things according to your needs, but for this matter it works as intended. To make a good use of the CURL request and not make and use repetitive code, I would put it in a function, in this case MakeRequest($endpoint, $data)

Where $endpoint is a String and $data should be an Array

In order to open and send a direct message to a user, you need these endpoints.

For creating a new direct message

POST /users/@me/channels

For sending messages:

POST /channels/{channel.id}/messages

<?php
function MakeRequest($endpoint, $data) {
    # Set endpoint
    $url = "https://discord.com/api/".$endpoint."";

    # Encode data, as Discord requires you to send json data.
    $data = json_encode($data);

    # Initialize new curl request
    $ch = curl_init();
    $f = fopen('request.txt', 'w');

    # Set headers, data etc..
    curl_setopt_array($ch, array(
        CURLOPT_URL            => $url, 
        CURLOPT_HTTPHEADER     => array(
            'Authorization: Bot token',
            "Content-Type: application/json",
            "Accept: application/json"
        ),
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_VERBOSE        => 1,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_POSTFIELDS => $data
        CURLOPT_STDERR         => $f,
    ));

    $request = curl_exec($ch);
    curl_close($ch);
    return json_decode($request, true);
}

# Open the DM first
$newDM = MakeRequest('/users/@me/channels', array("recipient_id" => "ID From the user"));

# Check if DM is created, if yes, let's send a message to this channel.
if(isset($newDM["id"])) {
    $newMessage = MakeRequest("/channels/".$newDM["id"]."/messages", array("content" => "Hello World."));
}
?>

enter image description here

Heads up: Due security and privacy matters, a direct message might not open if:

  • The user doesn't share the same server as your bot.
  • The user has turned off DM's from server members.
  • The user has blocked your bot.
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!