Discord webhook embed php curl 在最近的 Discord 更新后不再工作

Posted

技术标签:

【中文标题】Discord webhook embed php curl 在最近的 Discord 更新后不再工作【英文标题】:Discord webhook embed php curl not working anymore after recent Discord update 【发布时间】:2020-04-03 14:13:36 【问题描述】:

我使用这个 Discord 网络钩子很长时间了,一切都很好。现在它突然停止工作了。我收到的错误是:

"message": "无法发送空消息", "code": 50006

当消息嵌入时。这是我的代码:

$url = "https://discordapp.com/api/webhooks/1234567890/ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$hookObject = json_encode([
    "content" => "New sellers application!",
    "username" => "Roblox",
    "avatar_url" => "https://www.roblox.com/headshot-thumbnail/image?userId=1&width=420&height=420&format=png",
    "tts" => false,
    "embeds" => [
        [
            "title" => "Accept",
            "type" => "rich",
            "description" => "",
            "url" => "https://google.com/",
            "timestamp" => gmdate('Y-m-d\TH:i:s', time()),
            "color" => hexdec( "02b757" ),
            "author" => [
                "name" => "REMOVED",
                "url" => "REMOVED"
            ],
            "fields" => [
                [
                    "name" => "Discord",
                    "value" => "REMOVED#0563",
                    "inline" => true
                ],
                [
                    "name" => "Amount per week",
                    "value" => 30000,
                    "inline" => true
                ],
                [
                    "name" => "Email",
                    "value" => "REMOVED@example.com",
                    "inline" => false
                ],
                [
                    "name" => "Name",
                    "value" => "hehe",
                    "inline" => true
                ],
                [
                    "name" => "Roblox username",
                    "value" => "Roblox",
                    "inline" => true
                ],
                [
                    "name" => "Roblox link",
                    "value" => "https://www.roblox.com/users/1/profile",
                    "inline" => false
                ],
                [
                    "name" => "Decline ->",
                    "value" => "https://google.com/api/decseller?id=JUSTTESTJUSTTEST",
                    "inline" => false
                ]
            ]
        ]
    ]

], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
echo $hookObject;
$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"
    ]
]);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec( $ch );
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
echo "Body -> ";
$body = substr($response, $header_size);
var_dump($body);
curl_close($ch);

该代码返回以下内容:

"content":"New sellers application!","username":"Roblox","avatar_url":"https://www.roblox.com/headshot-thumbnail/image?userId=1&width=420&height=420&format=png","tts":false,"embeds":["title":"Accept","type":"rich","description":"","url":"https://google.com/","timestamp":"2019-12-10T10:23:33","color":178007,"author":"name":"REMOVED","url":"REMOVED","fields":["name":"Discord","value":"REMOVED#0563","inline":true,"name":"Amount per week","value":"30000","inline":true,"name":"Email","value":"REMOVED@example.com","inline":false,"name":"Name","value":"hehe","inline":true,"name":"Roblox username","value":"Roblox","inline":true,"name":"Roblox link","value":"https://www.roblox.com/users/1/profile","inline":false,"name":"Decline ->","value":"https://google.com/api/decseller?id=JUSTTESTJUSTTEST","inline":false]]* Expire in 0 ms for 6 (transfer 0x1c517af4500)
* Expire in 1 ms for 1 (transfer 0x1c517af4500)
* Expire in 0 ms for 1 (transfer 0x1c517af4500)
* Expire in 2 ms for 1 (transfer 0x1c517af4500)
* Expire in 1 ms for 1 (transfer 0x1c517af4500)
* Expire in 1 ms for 1 (transfer 0x1c517af4500)
* Expire in 4 ms for 1 (transfer 0x1c517af4500)
* Expire in 2 ms for 1 (transfer 0x1c517af4500)
* Expire in 2 ms for 1 (transfer 0x1c517af4500)
* Expire in 5 ms for 1 (transfer 0x1c517af4500)
*   Trying 162.159.134.233...
* TCP_NODELAY set
* Expire in 149992 ms for 3 (transfer 0x1c517af4500)
* Expire in 200 ms for 4 (transfer 0x1c517af4500)
* Connected to discordapp.com (162.159.134.233) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: OU=Domain Control Validated; OU=PositiveSSL Multi-Domain; CN=ssl711320.cloudflaressl.com
*  start date: Sep  5 00:00:00 2019 GMT
*  expire date: Mar 13 23:59:59 2020 GMT
*  issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO ECC Domain Validation Secure Server CA 2
*  SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x1c517af4500)
> POST /api/webhooks/1234567890/ABCDEFGHIJKLMNOPQRSTUVWXYZ HTTP/2
Host: discordapp.com
Accept: */*
Content-Length: 859
Content-Type: application/x-www-form-urlencoded

* old SSL session ID is stale, removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 256)!
* We are completely uploaded and fine
< HTTP/2 400
< date: Tue, 10 Dec 2019 10:23:39 GMT
< content-type: application/json
< content-length: 58
< set-cookie: __cfduid=dc03695b32b9ae182c651991a4b50bbe51575973419; expires=Thu, 09-Jan-20 10:23:39 GMT; path=/; domain=.discordapp.com; HttpOnly
< strict-transport-security: max-age=31536000; includeSubDomains
< x-ratelimit-bucket: 3cd1f278bd0ecaf11e0d2391374c011d
< x-ratelimit-limit: 5
< x-ratelimit-remaining: 4
< x-ratelimit-reset: 1575973422
< x-ratelimit-reset-after: 2
< via: 1.1 google
< alt-svc: clear
< cf-cache-status: DYNAMIC
< expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
< server: cloudflare
< cf-ray: 542e812f4e54d905-AMS
<
* Connection #0 to host discordapp.com left intact
Body -> string(58) ""message": "Cannot send an empty message", "code": 50006"

我在 Reddit 上问过,那里的人(显然)帮不了我,我在以下 Discord 公会问过:

Discord 官方 API 编码中心

但也没有人能提供帮助,所以我现在 100% 依赖你们,关于 Discord webhooks 的文档 -> https://discordapp.com/developers/docs/resources/webhook#execute-webhook

感谢您的宝贵时间!

【问题讨论】:

你的嵌入对象中的description不应该是非空的吗?我很确定那是实际的信息。见Discord Webhook。 【参考方案1】:

您现在需要发送 content-type,没有错误,并且 disord 在他们的文档中没有提到这一点。

Content-length 现在似乎是可选的,但也许以后他们也可能需要它。

<?php
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
        'Content-Type: application/json',
        // 'Content-Length: ' . strlen($data_string)
    ));

【讨论】:

如果程序员没有手动指定Content-Length,libcurl会在CURLOPT_POST请求时自动生成header。并且让 curl 自动生成标题更安全(curl 不会打错字或忘记空格或其他东西,他们有测试套件来确保这一点,但程序员可能会忘记空格或打错字或其他东西)经验法则:不要自己制作 Content-Length 标头,让 libcurl 为您完成。 我的回答是针对不和谐的,也许他们的服务器很可能需要它。我们的机器人停止工作,所以我尝试了提供和不提供内容类型。这就是我们当时的问题。 我只是在评论你的Content-length seems optional now but maybe later on they might require that too.,而不是“Content-Type”,你不应该手动提供 Content-Length ^^ 如果你不这样做,libcurl 会为你做自己。 @AaronJonk 尝试将您的函数精简为一个基本的简单消息,并将其与内容类型 json 混合,看看它是否有效。也有可能是 UTF8 出现在 1 个变量上。还要删除您的“长度”标题部分。

以上是关于Discord webhook embed php curl 在最近的 Discord 更新后不再工作的主要内容,如果未能解决你的问题,请参考以下文章

使用变量作为掩码链接; discord.py 嵌入通过 webhook 发布; Python

在 PHP 中使用 webhook 将文件上传到 Discord

discord.Embed 不被视为嵌入 discord.py

Discord.js:检测来自 Webhook 的消息

discord.py,使用 webhook 在所有通道中发送消息

查找和删除 Discord webhook 消息