使用 PHP 中的 webhook 将文件上传到 Discord [英] Uploading file to Discord with webhook in PHP

查看:24
本文介绍了使用 PHP 中的 webhook 将文件上传到 Discord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我所拥有的

<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: x-user-id");
header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');

if (!isset(getallheaders()["X-User-ID"]) || !isset($_FILES["audio"])) {
    return;
}

$webhookurl = "discord url";
//$webhookurl = "https://8f13d44091009d558421159246966183.m.pipedream.net";

$json_data = [
    "content" => "<@".getallheaders()["X-User-ID"].">",
    "tts" => false,
    "file" => curl_file_create($_FILES["audio"]["tmp_name"], "audio/mpeg", "sound.mp3") // "@".realpath($_FILES["audio"]["tmp_name"])
];

if (isset(getallheaders()["X-User-ID"])) {
    $curl = curl_init( $webhookurl );
    curl_setopt($curl, CURLOPT_TIMEOUT, 5); // 5 seconds
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5); // 5 seconds
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($json_data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ));
    
    curl_exec( $curl );
    curl_close( $curl );
}

我得到的只是带有 content" 字段(在本例中提及)的消息,但没有文件.嵌入也能正常工作.

All I get is a message with the "content" field (a mention in this case), no file though. Embeds work fine too.

这是我发现的:

  • 来自 Discord 文档:file file contents 被发送的文件的内容是 content、file、embeds 之一(非常没用).
  • 这里的这个人有代码用于上传文件.我复制了他的 cURL 标题,没有骰子.
  • 这个页面说你必须使用<代码>多部分/表单数据.Discord 文档说你只需要将它用于嵌入.我发现这两者都不是真的.嵌入在 application/json 中运行良好,但我无法使用 multipart/form-data 获得消息.
  • From Discord docs: file file contents the contents of the file being sent one of content, file, embeds (pretty useless).
  • This guy here has code for uploading a file. I have copied his cURL headers, no dice.
  • This page says that you have to use multipart/form-data. The Discord docs say you only have to use it for embeds. Neither of which I found to be true. Embeds work fine with application/json, and I can't get a message at all with multipart/form-data.

我还尝试了什么:

  • curl_file_create() 换成旧的 @/path/to/file(参见上面注释掉的部分)
  • 不同的Content-Type,就像我说的.
  • file_get_contents()
  • move_uploaded_file() 而不是直接从 $_FILES 开始.
  • Swapping out curl_file_create() for the older @/path/to/file (see commented out above)
  • Different Content-Type, like I said.
  • file_get_contents()
  • move_uploaded_file() instead of going directly from $_FILES.

对我缺少的东西有什么想法吗?我想我的主要问题是我不确定 Discord 期望什么格式...

Any ideas on what I'm missing? I guess my main problem is I'm not sure what format Discord expects...

推荐答案

想通了.我不应该在 body 上使用 json_encode(),我 应该使用 multipart/form-data.我还必须调整一些字段.

Figured it out. I shouldn't have been using json_encode() on the body, and I was supposed to use multipart/form-data. I also had to adjust some fields.

这篇关于使用 PHP 中的 webhook 将文件上传到 Discord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆