我想通过令牌而不是机器人和 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);
上层返回未授权代码
答案 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'])
]);
正如视频中提到的,这只是一个演示。对于生产代码,你就必须做更多的事情。至少:
问题是关于 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);