【问题标题】:How to send a Embedded Webhook using PHP [Discord]如何使用 PHP [Discord] 发送嵌入式 Webhook
【发布时间】:2019-01-15 18:52:16
【问题描述】:

当用户在我的网站上填写表单时,我正尝试将 Webhook 发送到 Discord 频道,我真的很想嵌入它,但是我在这样做时遇到了麻烦。我已经设法使用“内容”很好地发布了 Webhook,但是我无法将它嵌入它。

$Embed = {
  "username": "Kick Report",
  "embeds": [{
    "fields": [
      {
        "name": "Victim",   
        "value": "Change Victim Later",
        "inline": true
      },
      {
        "name": "Reason",
        "value": "Change Reason Later!",
        "inline": true
      },
      {
        "name": "Caller",
        "value": "Change Caller Later"
      },
      {
        "name": "Date",
        "value": "Change Date Later"
      }
    ]
  }]
};


$data = array("content" => $Embed, "Kick Report" => "Webhooks");
$curl = curl_init("https://discordapp.com/api/webhooks/xxxxxxxxxxxxxxxxxxxxxxxx");
 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
 return curl_exec($curl);

【问题讨论】:

    标签: php json webhooks discord


    【解决方案1】:

    试试这个代码:

    <?php
    
    // Replace the URL with your own webhook url
    $url = "https://discordapp.com/api/webhooks/0000000/ABCDEFGH....";
    
    $hookObject = json_encode([
        /*
         * The general "message" shown above your embeds
         */
        "content" => "A message will go here",
        /*
         * The username shown in the message
         */
        "username" => "MyUsername",
        /*
         * The image location for the senders image
         */
        "avatar_url" => "https://pbs.twimg.com/profile_images/972154872261853184/RnOg6UyU_400x400.jpg",
        /*
         * Whether or not to read the message in Text-to-speech
         */
        "tts" => false,
        /*
         * File contents to send to upload a file
         */
        // "file" => "",
        /*
         * An array of Embeds
         */
        "embeds" => [
            /*
             * Our first embed
             */
            [
                // Set the title for your embed
                "title" => "Google.com",
    
                // The type of your embed, will ALWAYS be "rich"
                "type" => "rich",
    
                // A description for your embed
                "description" => "",
    
                // The URL of where your title will be a link to
                "url" => "https://www.google.com/",
    
                /* A timestamp to be displayed below the embed, IE for when an an article was posted
                 * This must be formatted as ISO8601
                 */
                "timestamp" => "2018-03-10T19:15:45-05:00",
    
                // The integer color to be used on the left side of the embed
                "color" => hexdec( "FFFFFF" ),
    
                // Footer object
                "footer" => [
                    "text" => "Google TM",
                    "icon_url" => "https://pbs.twimg.com/profile_images/972154872261853184/RnOg6UyU_400x400.jpg"
                ],
    
                // Image object
                "image" => [
                    "url" => "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
                ],
    
                // Thumbnail object
                "thumbnail" => [
                    "url" => "https://pbs.twimg.com/profile_images/972154872261853184/RnOg6UyU_400x400.jpg"
                ],
    
                // Author object
                "author" => [
                    "name" => "Alphabet",
                    "url" => "https://www.abc.xyz"
                ],
    
                // Field array of objects
                "fields" => [
                    // Field 1
                    [
                        "name" => "Data A",
                        "value" => "Value A",
                        "inline" => false
                    ],
                    // Field 2
                    [
                        "name" => "Data B",
                        "value" => "Value B",
                        "inline" => true
                    ],
                    // Field 3
                    [
                        "name" => "Data C",
                        "value" => "Value C",
                        "inline" => true
                    ]
                ]
            ]
        ]
    
    ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
    
    $ch = curl_init();
    
    curl_setopt_array( $ch, [
        CURLOPT_URL => $url,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $hookObject,
        CURLOPT_HTTPHEADER => [
            "Content-Type: application/json"
        ]
    ]);
    
    $response = curl_exec( $ch );
    curl_close( $ch );
    
    ?>
    

    来源:https://www.reddit.com/r/discordapp/comments/83itgm/does_anyone_know_how_to_send_embeds_from_php_to_a/

    【讨论】:

    • 什么令牌?你确定我需要这样做吗?当我不使用嵌入时,它可以正常工作,而只需输入诸如“这是一个测试”之类的内容。
    • 对不起,如果这有点烦人,但我应该把这个放在哪里?
    • 好的,现在可以了!现在唯一的问题是如何让每个字段的“值”显示某个变量中的内容,例如:"fields" =&gt; [ [ "name" =&gt; "Victim", "value" =&gt; $Victim, "inline" =&gt; true ], 如何让“值”成为 $Victim 变量中的任何内容?
    • $Victim 是数组还是对象?如果使用数组,只需获取密钥,$Victim['data']
    • 这不再起作用,因为您没有正确使用标题。它必须是CURLOPT_HTTPHEADER =&gt; ["Content-Type: application/json"]
    猜你喜欢
    • 2020-04-11
    • 2020-07-08
    • 2020-11-26
    • 2020-07-10
    • 2020-09-25
    • 2021-10-21
    • 2021-10-18
    • 2021-02-14
    • 2020-10-11
    相关资源
    最近更新 更多