gpt4 book ai didi

php - 通过 discord webhook 发送附件/文件

转载 作者:行者123 更新时间:2023-12-05 05:52:50 29 4
gpt4 key购买 nike

我一直在尝试弄清楚如何在 webhook 中发送附件。我已经尝试了几个小时,但无法弄清楚。我一直在阅读 this但我仍然没有太多联系。我也一直在阅读 docs但很难理解。谁能帮助我?

<pre><?php 

$url = "WEBHOOK URL HERE";

$headers = [ 'Content-Type: multipart/form-data; charset=utf-8' ];
$POST = [
// Message
"content" => "Hello World!",

// Username
"username" => "testuser",

// File upload
"file" => curl_file_create("image.gif", 'image/gif', 'image')

];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($POST));
$response = curl_exec($ch);
var_dump($response);

我遇到了错误

"{"message": "Cannot send an empty message", "code": 50006}"

最佳答案

替换

"file" => curl_file_create("image.gif", 'image/gif', 'image.gif')

"file" => curl_file_create(realpath("image.gif"), 'image/gif', 'image.gif')
<pre><?php 

$url = "WEBHOOK URL HERE";

$headers = [ 'Content-Type: multipart/form-data; charset=utf-8' ];
$POST = [
// Message
"content" => "Hello World!",

// Username
"username" => "testuser",

// File upload
"file" => curl_file_create(realpath("image.gif"), 'image/gif', 'image.gif')

];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($POST));
$response = curl_exec($ch);
var_dump($response);

以上代码只是将你自己的服务器文件上传到discord。

如果您想从其他 URL 获取文件并上传到 Discord,那么您需要先将相同的文件上传到您自己的服务器上作为临时文件,然后在此处设置此临时文件..

"file" => curl_file_create(realpath($tempfile), 'image/gif', 'image.gif')

之后

$response   = curl_exec($ch);
unlink($tempfile); //delete temp file if you want.

关于php - 通过 discord webhook 发送附件/文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70028361/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com