【问题标题】:Upload a file to slack with files.upload in PHP使用 PHP 中的 files.upload 将文件上传到 slack
【发布时间】:2018-11-09 15:56:08
【问题描述】:

我正在尝试使用 files.upload 方法将文件上传到 Slack,但到目前为止我只收到一条空白消息。

这是代码,我在互联网上找到了这个脚本,但它不起作用。

<?php
include '../connessione.php';

$slacktoken = "myToken"; //this is not the real token ofc
$header = array();
$header[] = 'Content-Type: multipart/form-data';
$file = new CurlFile('../tmp/slack_icon.png', 'image/png');

$postitems =  array(
    'token' => $slacktoken,
    'channels' => "test_channel",
    'file' =>  $file,
    'text' => "This is my photo",
    'title' => "Photo title",
    'filename' => "myIcon.jpg"
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_URL, "https://slack.com/api/files.upload");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$postitems);

//Execute curl and store in variable
$data = curl_exec($curl);
?>

我做错了什么?

谢谢。

编辑:经过大量测试,如果我回显 $data 变量,我会收到此消息:

{"ok":false,"error":"missing_scope","needed":"files:write:user","provided":"identify,commands"}

更新 我将在这里发布完整的工作代码。

<?php
include '../connessione.php';

    // Buffer all upcoming output...
    ob_start();

    // Send your response.
    echo "Here be response";

    // Get the size of the output.
    $size = ob_get_length();

    // Disable compression (in case content length is compressed).
    header("Content-Encoding: none");

    // Set the content length of the response.
    header("Content-Length: {$size}");

    // Close the connection.
    header("Connection: close");

    // Flush all output.
    ob_end_flush();
    ob_flush();
    flush();

    // Close current session (if it exists).
    if(session_id()) session_write_close();


$channel = $_POST['channel_id'];
$slacktoken = "myToken";
$header = array();
$header[] = 'Content-Type: multipart/form-data';
$file = new CurlFile('slack_icon.png', 'image/png');



$postitems =  array(
        'token' => $slacktoken,
        'channels' => "test_channel",
        'file' =>  $file,
        'text' => "This is my photo",
        'title' => "Photo title",
        'filename' => "myIcon.jpg"
    );

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_URL, "https://slack.com/api/files.upload");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postitems);

//Execute curl and store in variable
$data = curl_exec($curl);
?>

开头部分取自continue processing php after sending http response。这需要立即回复 Slack 并避免“已达到超时”消息。

【问题讨论】:

  • 欢迎您!很遗憾,如果没有错误消息,我们将无法为您提供帮助。如果出现空白屏幕,请尝试从页面末尾删除 ?&gt;,然后重试。确保你有error reporting turned on。否则我们不知道问题可能是什么。祝你好运!
  • @ThomasEdwards 你好!我打开了错误报告,但如果我去网页,我仍然会得到一个空白屏幕。我也尝试删除?&gt;,但没有。
  • 如果我通过斜杠命令运行脚本,我的 slack 应用程序会收到一条空白消息
  • 显然您的令牌没有所需的范围:您需要将files:write:user 范围添加到您的令牌
  • @ErikKalkoken 我该怎么做?

标签: php slack slack-api


【解决方案1】:

缺少作用域

您的令牌缺少必要的范围,此处:files:write:user

要添加额外的范围:

假设您有一个 Slack 应用程序,请转到您的应用程序的管理页面,然后在“Ouath”子页面上添加它,您可以在其中添加它的范围部分。这是指向您的应用程序的直接链接:https://api.slack.com/apps

之后,您需要将应用重新安装到工作区,以便更改生效。

使用 CurlFile

另一个潜在的陷阱是CurlFile 显然只适用于文件的绝对路径。 (see this answer)。快速修复将是这样的:

$file = new CurlFile(realpath(dirname(__FILE__) . '/../tmp/slack_icon.png'), 'image/png');

【讨论】:

  • 我创建了一个机器人用户,现在我有了另一个 OAuth 令牌。现在使用这个新令牌我得到了这个{"ok":false,"error":"no_file_data"}
  • 好吧,现在我得到了这个{"ok":true,"file":{"id":"FDZF1LH0R","created":1541785891,"timestamp":1541785891,"name":"myIcon.jpg".....我剪掉了所有的字符串,因为它太长了,但它似乎正在工作,但如果我通过 slack slash 命令运行它,我会得到一个超时
  • 酷。然后我会说最初的问题已经得到解答。如何使用斜杠上的 3 秒超时命令它是另一个主题。并且已经在这里回答了:stackoverflow.com/questions/36195924/…
  • 我尝试添加 ignore_user_abort(true); ob_start(); echo('{"response_type": "in_channel", "text": "Checking, please wait..."}'); header($_SERVER["SERVER_PROTOCOL"] . " 200 OK"); header("Content-Type: application/json"); header('Content-Length: '.ob_get_length()); ob_end_flush(); ob_flush(); flush(); 但它仍然给我一个超时错误。但是,上传工作正常,并且文件无论如何都会显示在 slack 上。所以这不仅仅是细节问题
  • 我也从来没有让它与缓冲区捕获方法 (ob_start(), ..) 一起工作。但是请检查我为基于 curl 的方法链接的问题。那个效果很好。
猜你喜欢
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多