在我的不和谐网络钩子中,我收到错误:{"embeds": ["0"]}

3 php discord

我正在制作一个不和谐的 webhook 来记录一些东西,我是在模板的帮助下完成的(我不擅长 php),但我不断收到错误消息: {"embeds": ["0"]}

我已经尝试过研究它,我没有得到任何有用的东西。请注意我这样做是为了测试的混乱。

这是我的代码:

<?php
  $url = "https://discordapp.com/api/webhooks/xxx"; // Censored for privacy

  $hookObject = json_encode([
      "username" => "Promotion Logs",
      "avatar_url" => "https://cdn.discordapp.com/icons/472520717515096078/60cc7dd2864c95a749516d1213359b67.png",
      "tts" => false,
      "embeds" => [
          [
              "title" => "Promotion Logs",
              "type" => "rich",
              "description" => "",
              "url" => "http://police.riverside-roleplay.com/promologs.php",
              "color" => hexdec( "0099ff" ),
              "fields" => [
                  [
                      "name" => "Name",
                      "value" => "dd",
                      "inline" => false
                  ],
                  [
                      "name" => "Rank",
                      "value" => "$rank",
                      "inline" => true
                  ],
                  [
                    "name" => "Their name",
                    "value" => "dd",
                    "inline" => true
                ],
                [
                  "name" => "Old rank",
                  "value" => "dd",
                  "inline" => true
              ],
              [
                "name" => "New rank",
                "value" => "dd",
                "inline" => true
            ],
            [
              "name" => "Reason",
              "value" => "dd",
              "inline" => true
          ],
          [
            "name" => "Date",
            "value" => "dd",
            "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 => [
          "Length" => strlen( $hookObject ),
          "Content-Type" => "application/json"
      ]
  ]);

  $response = curl_exec( $ch );
  curl_close( $ch );

  ?>
Run Code Online (Sandbox Code Playgroud)

这是我使用的模板:

<?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 => [
        "Length" => strlen( $hookObject ),
        "Content-Type" => "application/json"
    ]
]);

$response = curl_exec( $ch );
curl_close( $ch );

?>
Run Code Online (Sandbox Code Playgroud)

我的预期结果是嵌入我设置的字段。我之前有这个,然后我将它实现到不同的页面并更改了一些东西,然后它被破坏了。实际结果是错误{"embeds": ["0"]}

Yay*_*H48 5

您的代码的主要问题似乎$rank是未定义,因为其中没有值,因此"value" => "$rank",导致问题,因为 Discord 期望其中有值。

使用您编写的代码(并将 webhook 链接替换为用于测试的功能链接)最初会出现不同的错误:

{"message": "Cannot send an empty message", "code": 50006}

这可能是固定的,通过改变这一点:

      CURLOPT_HTTPHEADER => [
          "Length" => strlen( $hookObject ),
          "Content-Type" => "application/json"
      ]
Run Code Online (Sandbox Code Playgroud)

进入这个:

      CURLOPT_HTTPHEADER => [
          "Length" => strlen( $hookObject ),
          "Content-Type: application/json"
      ]
Run Code Online (Sandbox Code Playgroud)

在更改了该位之后,它现在确实{"embeds": ["0"]}按照您的描述给出了错误。在我为$rank顶部定义一些值后,您的代码将再次运行。所以实际的问题是该值是未定义的。

在我的 Gist 上查看原始代码和编辑后的代码之间的完整文件比较:https : //gist.github.com/mnh48/a707aec600ac74f4e5d50875fcab5d7c

我以 34.2 为例$rank = 34.2;,结果如下:

结果


对于来自搜索引擎的其他人

我最初遇到了同样的错误,我已经找到了原因,但我这边的原因与 OP 遇到的不同。

每当有人从 Discord webhook 收到此回复时:

{"embeds": ["0"]}
Run Code Online (Sandbox Code Playgroud)

它可能意味着两件不同的事情:

  1. 正文中发送了无效字段
  2. 某些元素已超出限制

对于第一种情况,它可以是:

  1. 格式错误:

    • 格式错误timestamp,必须是ISO8601格式
    • 中的格式错误color,必须是十进制,不接受其他任何内容(是的,甚至不接受颜色名称字符串"red",将它们转换为十进制 - 红色是16711680,您可以使用像SpyColor这样的站点来检查十进制值)
    • icon_url, avatar_url, or 中的格式错误url,所有这些都必须在http://, https://, 中(另外对于url期望图像,attachment://图像文件也附加到请求中的时间可能很长)
  2. 无效值:

    • 使用意外的值,例如传递字符串而不是布尔值 for inline(接受两个字符串,可能是出于兼容性原因:"true"and "false",但不接受任何其他字符串,并且它期待布尔值)
    • 空或空值,如果您希望它为空,则首先不要包含它,或_ _用作值
    • 未定义值,检查所有变量是否实际定义

对于第二种情况,限制如下:

  1. 嵌入标题的限制为 256 个字符
  2. 嵌入描述的限制为 2048 个字符
  3. 每个字段的名称限制为 256 个字符
  4. 每个字段的值限制为 1024 个字符
  5. 页脚文本的限制为 2048 个字符
  6. 作者姓名限制为 256 个字符
  7. 每条消息最多可以有 25 个字段
  8. 每条消息最多可以有 10 个嵌入
  9. 总字符总数不得超过 6000 个字符

就我而言,是后者。我将我的电子邮件客户端设置为将电子邮件的内容作为 Discord Webhook 在embeds> fields> 中发送value,并且它正在传递整个长电子邮件。该value字段的最大字符限制为 1024,但我的电子邮件内容消息有 1565 个字符。


Bea*_*lle 1

我遇到了同样的错误,我的问题是在 JSON 中使用环境变量。

如果 url 字段不是有效的 url,您将收到错误消息。

例如"url":"$SOME_VAR"

我相信其他类型也可能发生这种情况!