Advertisement
Mo45

PHP - Send message to Discord via Webhook

Jan 23rd, 2021 (edited)
4,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.36 KB | None | 0 0
  1. <?PHP
  2. //
  3. //-- https://gist.github.com/Mo45/cb0813cb8a6ebcd6524f6a36d4f8862c
  4. //
  5.     function discordmsg($msg, $webhook) {
  6.         if($webhook != "") {
  7.             $ch = curl_init( $webhook );
  8.             curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  9.             curl_setopt( $ch, CURLOPT_POST, 1);
  10.             curl_setopt( $ch, CURLOPT_POSTFIELDS, $msg);
  11.             curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
  12.             curl_setopt( $ch, CURLOPT_HEADER, 0);
  13.             curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
  14.  
  15.             $response = curl_exec( $ch );
  16.             // If you need to debug, or find out why you can't send message uncomment line below, and execute script.
  17.             echo $response;
  18.             curl_close( $ch );
  19.         }
  20.     }
  21.  
  22.     // URL FROM DISCORD WEBHOOK SETUP
  23.     $webhook = "hook_url";
  24.     $timestamp = date("c", strtotime("now"));
  25.     $msg = json_encode([
  26.     // Message
  27.     "content" => "Kek lol ;) <@75216985209700352>",
  28.  
  29.     // Username
  30.     "username" => "krasin.space",
  31.  
  32.     // Avatar URL.
  33.     // Uncomment to use custom avatar instead of bot's pic
  34.     //"avatar_url" => "https://ru.gravatar.com/userimage/28503754/1168e2bddca84fec2a63addb348c571d.jpg?size=512",
  35.  
  36.     // text-to-speech
  37.     "tts" => false,
  38.  
  39.     // file_upload
  40.     // "file" => "",
  41.  
  42.     // Embeds Array
  43.     "embeds" => [
  44.         [
  45.             // Title
  46.             "title" => "PHP - Send message to Discord (embeds) via Webhook",
  47.  
  48.             // Embed Type, do not change.
  49.             "type" => "rich",
  50.  
  51.             // Description
  52.             "description" => "Description will be here, someday <@75216985209700352>",
  53.  
  54.             // Link in title
  55.             "url" => "https://gist.github.com/Mo45/cb0813cb8a6ebcd6524f6a36d4f8862c",
  56.  
  57.             // Timestamp, only ISO8601
  58.             "timestamp" => $timestamp,
  59.  
  60.             // Left border color, in HEX
  61.             "color" => hexdec( "3366ff" ),
  62.  
  63.             // Footer text
  64.             "footer" => [
  65.                 "text" => "GitHub.com/Mo45",
  66.                 "icon_url" => "https://ru.gravatar.com/userimage/28503754/1168e2bddca84fec2a63addb348c571d.jpg?size=375"
  67.             ],
  68.  
  69.             // Embed image
  70.             "image" => [
  71.                 "url" => "https://ru.gravatar.com/userimage/28503754/1168e2bddca84fec2a63addb348c571d.jpg?size=600"
  72.             ],
  73.  
  74.             // thumbnail
  75.             //"thumbnail" => [
  76.             //    "url" => "https://ru.gravatar.com/userimage/28503754/1168e2bddca84fec2a63addb348c571d.jpg?size=400"
  77.             //],
  78.  
  79.             // Author name & url
  80.             "author" => [
  81.                 "name" => "krasin.space",
  82.                 "url" => "https://krasin.space/"
  83.             ],
  84.  
  85.             // Custom fields
  86.             "fields" => [
  87.                 // Field 1
  88.                 [
  89.                     "name" => "Field #1",
  90.                     "value" => "Value #1",
  91.                     "inline" => false
  92.                 ],
  93.                 // Field 2
  94.                 [
  95.                     "name" => "Field #2",
  96.                     "value" => "Value #2",
  97.                     "inline" => true
  98.                 ]
  99.                 // etc
  100.             ]
  101.         ]
  102.     ]
  103.  
  104. ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
  105.  
  106.     discordmsg($msg, $webhook); // SENDS MESSAGE TO DISCORD
  107.     echo "sent?";
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement