如何使用PHP发送discord webhook?

如何使用PHP发送discord webhook?,php,discord,Php,Discord,我试图让一个表单将信息发送到一个不协调的频道,但是我在建立webhook连接时遇到了问题,因为它一直在说{“message”:“不能发送空消息”,“code”:50006} 这是我的代码: $url = "https://discordapp.com/api/webhooks/xxxxxxxxx"; $hookObject = json_encode([ "content" => "A message will go here", "username" => "My

我试图让一个表单将信息发送到一个不协调的频道,但是我在建立webhook连接时遇到了问题,因为它一直在说
{“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 );

这应该是有效的:

$url=”https://discordapp.com/api/webhooks/xxxxxxxxx";
$headers=['内容类型:application/json;charset=utf-8'];
$POST=['username'=>'testingbot','content'=>'testingmessage'];
$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\u exec($ch);

Related请通过分享代码工作的原因以及您为使其工作所做的工作来帮助询问者理解您的答案。。这可能是一个安全风险!