带有 stream_context_create() 的 HTTPS 请求

HTTPS REQUEST WITH stream_context_create()

我的代码有问题,我得到的错误是:

Warning: fopen(https://discordapp.com/api/v6/auth/login): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST in C:\xampp\htdocs\s2.php on line 15

相关代码如下:

<?php
$data = array("email" => 'Email@gmail.com', "password" => 'Password');
$data_string = json_encode($data);                                                                                   
$context_options = array (
        'https' => array (
            'method' => 'POST',
            'header'=> "Content-type: application/json\r\n"
                . "Content-Length: " . strlen($data_string). "\r\n",
                 'content' => $data_string

            )
        );

$context = stream_context_create($context_options);
$fp = fopen('https://discordapp.com/api/v6/auth/login', 'r', false, $context);

?>

感谢您的帮助!

似乎不​​再允许通过机器人直接登录,因为您应该使用 Discord 的 OAuth2 (how does OAuth2 work?) 功能。这意味着您的机器人需要在您的 Discord 帐户中进行设置,然后您可以在机器人上使用基于令牌的访问来根据 Discord 对外部应用程序进行身份验证。 不再允许基于机器人的登录的更改发生在 2017 年初左右,那时所有基于 PHP 的 Discord 相关 Github 应用程序都停止维护。 Here is a discussion and comment about banning bots with automated login and this one关于必须使用 OAuth2。

阅读 the Discord OAuth2 chapter 中有关身份验证和机器人程序的更多信息。

如果您能详细说明您计划实现的目标,也许我们可以帮助您找到任务的解决方案。


上一个(不再有效)答案:

我没有使用过 DiscordApp,所以我还没有尝试过。您是否尝试将值作为 FORM 值发布到 API?

而不是发送 JSON 编码的数据
$postData = array(
  'email' => 'Email@gmail.com', 
  'password' => 'Password'
);
$params = array('https' =>
  array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => $postData
  )
);

$context  = stream_context_create($params);

// get the entire response ...
$result = file_get_contents('https://discordapp.com/api/v6/auth/login', false, $context);

// ... or alternatively using fopen()
$fp = fopen('https://discordapp.com/api/v6/auth/login', 'r', false, $context);

我还没有找到任何关于如何将参数传递给 login URI 的文档,但至少这是可以使用正常的基于表单的登录方式。

根据 DiscordApp Response codes 如果调用不被理解(调用的函数不存在)或参数发送不正确,则可能会收到 400,因此您应该了解如何调用 login使用脚本的参数接口。