我一直在努力弄清如何在Webhooks中发送附件。我已经尝试了数小时,但无法搞清楚。我已经阅读了这篇文章,但仍然不太理解。我还阅读了文档,但很难理解。有人能帮助我吗?
<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}"
Content-Type
头,让cURL处理它。multipart/form-data
请求需要在各个部分之间有一个边界,而这个边界字符串需要包含在头部中。但是你指定的缺失了,所以接收者将不知道如何解释此请求。 - CBroe