我想要帮助制作不和谐卷曲

时间:2021-04-08 03:34:48

标签: php curl discord

我想通过令牌而不是机器人和 curl 来发送消息并获取它 像python中的这个 Using Python Requests to Send Discord Messages

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://discord.com/api/v8/channels/YOURID/messages"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'authorization' => 'YOURTOKEN']);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode(['content' => 'hello world']));
curl_exec($ch);

上层返回未授权代码

1 个答案:

答案 0 :(得分:0)

使用GuzzleHttp。它的语法更接近pythons的请求,然后直接使用curl。

<?php
use GuzzleHttp\Client;

$client = new Client();
$response = $client->post( 'https://discord.com/api/v8/channels/YOURID/messages', [
      'headers' => [ 'authorization' => 'YOURTOKEN'],
      'body' => json_encode(['content' => 'hello world'])
]);

正如视频中提到的,这只是一个演示。对于生产代码,你就必须做更多的事情。至少:

  • 自定义界面来表达您的目标,而不是您在做什么
  • channelId 和您的 authToken 不应存储在您的代码中,而应来自环境,甚至更好地来自某种保险库。
  • 的异常处理被完全丢失

问题是关于 curl 而不是 Guzzle。所以这是一个纯卷曲解决方案。 documentation on PHP's curl 模块很好,这里实际上包含一个 similar answer。像上面这是一个非常梗概答案

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://discord.com/api/v8/channels/YOURID/messages"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'authorization' => 'YOURTOKEN']);
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode(['content' => 'hello world']));
curl_exec($ch);