首页 > 解决方案 > 简单的 PHP 不和谐通知现在响应错误代码 50006

问题描述

我有一个简单的每日 PHP 通知到一个不和谐的 webhook。它已经工作了将近一年,但现在它给我一个错误:

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

$content在之前创建并填充,并且它不是空的。我在这里替换了真实的用户名和头像链接。

$hookObject = json_encode([
    "type" => "rich",
    "content" => "**Rotation today**\n\n".$content,
    "username" => "avatar",
    "avatar_url" => "link to avatar",
    "tts" => false,
], 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 );

有谁知道现在可能是什么问题?

标签: phpdiscordwebhookserror-code

解决方案


我昨天也收到了这个错误。我找到了这段代码并且它有效:

//===========================================
// Create new webhook in your Discord channel settings and copy&paste URL
//===========================================
$webhookurl = "YOUR_WEBHOOK_URL";

//===========================================
// Compose message. You can use Markdown
// Message Formatting -- https://discordapp.com/developers/docs/reference#message-    formatting
//===========================================
$msg = "Test **message** ";
$json_data = array ('content'=>"$msg");
$make_json = json_encode($json_data);
$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, $make_json);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );

推荐阅读