我正在制作一个不和谐的 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"]}
您的代码的主要问题似乎$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)
它可能意味着两件不同的事情:
格式错误:
无效值:
inline(接受两个字符串,可能是出于兼容性原因:"true"and "false",但不接受任何其他字符串,并且它期待布尔值)_ _用作值就我而言,是后者。我将我的电子邮件客户端设置为将电子邮件的内容作为 Discord Webhook 在embeds> fields> 中发送value,并且它正在传递整个长电子邮件。该value字段的最大字符限制为 1024,但我的电子邮件内容消息有 1565 个字符。
我遇到了同样的错误,我的问题是在 JSON 中使用环境变量。
如果 url 字段不是有效的 url,您将收到错误消息。
例如"url":"$SOME_VAR"
我相信其他类型也可能发生这种情况!