问题描述
我目前正尝试在我的Discord频道上发布一条消息,以尝试使用cURL POST类型.我在运行代码时遇到的问题是,它给我一个401错误,表明我未经授权.我正在使用xampp localhost在Web服务器上运行PHP代码.我还尝试通过URL链接( https://discordapp.com/oauth2/authorize?client_id=MYAPPLICATIONID&scope=bot&permissions=8 ),并已成功将该漫游器添加到我的频道中.看看我的代码
I am currently trying to post a message on my Discord channel trying to use a cURL POST type. The issue that I am getting when I run my code is that it is giving me a 401 error saying I'm unauthorized. I am running my PHP code on a webserver using xampp localhost. I also went in and tried to authorize my application bot via URL link (https://discordapp.com/oauth2/authorize?client_id=MYAPPLICATIONID&scope=bot&permissions=8) and have successfully added the bot into my channel. Have a look at my code
$data = array("Authorization: Bot" => $clientSecret, 'content' => 'Test Message');
$data_string = json_encode($data);
$ch = curl_init('https://discordapp.com/api/v6/channels/'.$myChannel.'/messages');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$answer = curl_exec($ch);
echo $answer;
if (curl_error($ch)) {
echo curl_error($ch);
}
我从应用程序页面获取$ clientSecret来显示我的客户端秘密令牌,而$ myChannel是我的不和谐频道/服务器ID.
I get $clientSecret from the application page to reveal my client secret token and $myChannel is my discords channel/server id.
注意:我已经根据此处给出的另一个stackoverflow答案对代码进行了建模 discord php curl登录失败.因此,我不确定我是否为应用程序漫游器使用正确的语法
NOTE: I have modeled my code off another stackoverflow answer given here discord php curl login Fail . So I am unsure if I am using the correct syntax for the an application bot
推荐答案
此处为完整代码(不带cURL).只需将字符串WEBHOOK_HERE替换为机器人的Webhook:
Here is full code (Without cURL). Just replace the string WEBHOOK_HERE with your bot's webhook:
<?php
$message = $_POST['message'];
$data = ['content' => $message];
$options = [
'http' => [
'method' => 'POST',
'header' => 'Content-Type: application/json',
'content' => json_encode($data)
]
];
$context = stream_context_create($options);
$result = file_get_contents('WEBHOOK_HERE', false, $context);
?>
<form method="post">
Type your message here :<br><input type="text" name="message"><br>
<input type="submit" value="Submit">
</form>
我是新来的,希望您喜欢这段代码!
I'm new here so I hope you enjoy the code!
这篇关于使用cURL将不和谐发布消息发布到频道-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!