在 PHP 中使用 files.upload 将文件上传到 slack
Upload a file to slack with files.upload in PHP
我正在尝试使用 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"}
更新
我将在此处 post 提供完整的工作代码。
<?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);
?>
开头部分取自此。这需要立即回复 Slack 并避免 "Timeout was reached" 消息。
缺少范围
您的令牌缺少必要的范围,此处:files:write:user
。
要添加其他范围:
假设您有一个 Slack 应用程序,转到您的应用程序的管理页面,然后将其添加到 "Ouath" 子页面上,您可以在其中添加一个范围部分。这是直接 link 到您的应用程序:https://api.slack.com/apps。
之后您需要将您的应用程序重新安装到工作区,以便更改生效。
使用 CurlFile
另一个潜在的陷阱是 CurlFile
显然只适用于文件的绝对路径。 (see this answer)。快速修复是这样的:
$file = new CurlFile(realpath(dirname(__FILE__) . '/../tmp/slack_icon.png'), 'image/png');
我正在尝试使用 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"}
更新 我将在此处 post 提供完整的工作代码。
<?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);
?>
开头部分取自此。这需要立即回复 Slack 并避免 "Timeout was reached" 消息。
缺少范围
您的令牌缺少必要的范围,此处:files:write:user
。
要添加其他范围:
假设您有一个 Slack 应用程序,转到您的应用程序的管理页面,然后将其添加到 "Ouath" 子页面上,您可以在其中添加一个范围部分。这是直接 link 到您的应用程序:https://api.slack.com/apps。
之后您需要将您的应用程序重新安装到工作区,以便更改生效。
使用 CurlFile
另一个潜在的陷阱是 CurlFile
显然只适用于文件的绝对路径。 (see this answer)。快速修复是这样的:
$file = new CurlFile(realpath(dirname(__FILE__) . '/../tmp/slack_icon.png'), 'image/png');