【问题标题】:Discord Post Message To Channel Using cURL - PHPDiscord 使用 cURL 向频道发布消息 - PHP
【发布时间】:2019-01-31 21:28:15
【问题描述】:

我目前正在尝试使用 cURL POST 类型在我的 Discord 频道上发布消息。我在运行代码时遇到的问题是它给了我一个 401 错误,说我未经授权。我正在使用 xampp localhost 在网络服务器上运行我的 PHP 代码。我还尝试通过 URL 链接 (https://discordapp.com/oauth2/authorize?client_id=MYAPPLICATIONID&scope=bot&permissions=8) 授权我的应用程序机器人,并成功地将机器人添加到我的频道中。看看我的代码

$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。

注意:我已经根据此处给出的另一个 stackoverflow 答案建模了我的代码 discord php curl login Fail 。所以我不确定我是否为应用程序机器人使用了正确的语法

【问题讨论】:

  • 这里有一些看起来不对劲的地方,但由于我从未使用过 Discord,所以我不会在没有进一步验证的情况下进入它。不过,通过快速的 Google 搜索(和常见的 API 使用 exp),我发现您需要将授权作为标头而不是在正文有效负载中传递。因此,请尝试将 'Authorization: Bot '.$token, 添加到您的 CURLOPT_HTTPHEADER 数组中。 (见gist.github.com/ianklatzco/c033b33757915feaf48fd0caf14b42cb
  • 这个授权,需要使用TOKEN(不是clientSecret)。

标签: php curl discord


【解决方案1】:

这是完整的代码(没有 cURL)。只需将字符串 WEBHOOK_HERE 替换为您的机器人的 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>

我是新来的,希望你喜欢这段代码!

【讨论】:

    【解决方案2】:

    实际上,在没有使用 webhook 的情况下,我很难在网上搜索这个。我想将消息作为机器人发布,而不是 webhook。错误在于您的授权,因为它需要在标题中......所以我在这里为未来的谷歌人发布了正确的解决方案。

    <?php
    $myChannel = "CHANNEL_ID";
    $myToken = "TOKEN";
    $msg = 'Test Message';
    $data = array('content' => $msg);
    $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),
        'Authorization: Bot ' . $token
        )
    );
    
    $answer  = curl_exec($ch);
    
    echo $answer;
    
    if (curl_error($ch)) {
        echo curl_error($ch);
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      关闭!您的解决方案有错误。

      <?php
      $myChannel = "CHANNEL_ID";
      $myToken = "TOKEN";
      $msg = 'Test Message';
      $data = array('content' => $msg);
      $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),
          'Authorization: Bot ' . $myToken
          )
      );
      
      $answer  = curl_exec($ch);
      
      echo $answer;
      
      if (curl_error($ch)) {
          echo curl_error($ch);
      }
      ?>
      

      我刚刚在我的服务器上测试了它并且它工作正常。与 Bot ' 。 $token 失败了。

      【讨论】:

        猜你喜欢
        • 2021-03-24
        • 2020-04-07
        • 2021-01-10
        • 2020-10-01
        • 2019-03-01
        • 2021-03-27
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        相关资源
        最近更新 更多