Notification to Discord PHP

Notification to Discord PHP

If you need to send embed notification use this

PHP - Send message to Discord via Webhook
Send message to Discord via Webhook. GitHub Gist: instantly share code, notes, and snippets.

I only need a simple notification

$webhookurl = "https://discord.com/api/webhooks/*";
$array = ["will", "this", "show", "an", "array?"];

$json_data = json_encode([
    // Message
    "content" => json_encode($array),
    
    // Username
    "username" => "BOT",
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );


$ch = curl_init( $webhookurl );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec( $ch );
// If you need to debug, or find out why you can't send message uncomment line below, and execute script.
// echo $response;
curl_close( $ch );

Use Guzzle

use GuzzleHttp\Client;
$headers = [
    'Content-Type' => 'application/json',
];

$client = new Client([
    'headers' => $headers
]);
$array = ["this uses guzzle"];

$json_data = [
    // Message
    "content" => json_encode($array),
    
    // Username
    "username" => "BOT",
];

$response = $client->post('https://discord.com/api/webhooks/*', [
    'json' => $json_data
]);

Subscribe to You Live What You Learn

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe