1

I'm trying to make a form send the information to a discord channel, but im having trouble making the webhook connection since it just keeps saying {"message": "Cannot send an empty message", "code": 50006} This is my code:

$url = "https://discordapp.com/api/webhooks/xxxxxxxxx";

$hookObject = json_encode([
    "content" => "A message will go here",
    "username" => "MyUsername",
], JSON_FORCE_OBJECT);

$ch = curl_init();
var_dump($hookObject);
curl_setopt_array( $ch, [
    CURLOPT_URL => $url,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $hookObject,
    CURLOPT_HTTPHEADER => [
        "Length" => strlen( $hookObject ),
        "Content-Type" => "application/json"
    ]
]);

$response = curl_exec( $ch );
curl_close( $ch );
Todoe56
  • 21
  • 1
  • 3

1 Answers1

2

This should be working:

$url = "https://discordapp.com/api/webhooks/xxxxxxxxx";
$headers = [ 'Content-Type: application/json; charset=utf-8' ];
$POST = [ 'username' => 'Testing BOT', 'content' => 'Testing message' ];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($POST));
$response   = curl_exec($ch);
Syntle
  • 5,070
  • 3
  • 11
  • 34
  • Please help the person asking to understand your answer by sharing why your code will work, and what you've done to make it work. – Unbranded Manchester Jun 18 '20 at 09:56
  • 1
    **[You should not switch off `CURLOPT_SSL_VERIFYHOST` or `CURLOPT_SSL_VERIFYPEER`](https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software)**. It could be a security risk! [Here is how to get the certificate bundle if your server is missing one](https://stackoverflow.com/a/32095378/1839439) – Dharman Jun 19 '20 at 14:51