【问题标题】:i want help to make discord curl我需要帮助使不和谐卷曲
【发布时间】:2021-07-03 21:51:17
【问题描述】:

我想通过令牌而不是机器人和 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);

上面的代码返回未授权

【问题讨论】:

  • 那么,你的question 是什么?
  • 你可以在下面看到它

标签: php curl discord


【解决方案1】:

使用GuzzleHttp。它的语法更接近于python的request,然后直接使用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。所以这是一个纯粹的 curl 解决方案。 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);

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 2021-07-20
    • 2021-01-16
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2010-09-29
    相关资源
    最近更新 更多