【问题标题】:How can I send a discord webhook using PHP?如何使用 PHP 发送不和谐的 webhook?
【发布时间】:2020-03-31 19:18:12
【问题描述】:

我正在尝试让表单将信息发送到不和谐频道,但我无法建立 webhook 连接,因为它一直在说 {"message": "Cannot send an empty message", "code": 50006} 这是我的代码:

$url = "https://discordapp.com/api/webhooks/xxxxxxxxx";

$hookObject = json_encode([
    "content" => "A message will go here",
    "username" => "MyUsername",
], JSON_FORCE_OBJECT);

$ch = curl_init();
var_dump($hookObject);
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 );

【问题讨论】:

标签: php discord


【解决方案1】:

这应该可以工作:

$url = "https://discordapp.com/api/webhooks/xxxxxxxxx";
$headers = [ 'Content-Type: application/json; charset=utf-8' ];
$POST = [ 'username' => 'Testing BOT', 'content' => 'Testing message' ];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($POST));
$response   = curl_exec($ch);

【讨论】:

猜你喜欢
  • 2021-04-30
  • 2023-02-15
  • 2021-11-06
  • 2021-05-19
  • 2021-06-03
  • 2021-05-28
  • 2018-10-29
  • 2019-07-05
  • 2021-04-07
相关资源
最近更新 更多