在PHP中使用files.Upload将文件上载到slack
我正在尝试使用files.upload方法将文件上载到Slack,但到目前为止,我只收到一条空白消息 这是代码,我在网上找到了这个脚本,但它不起作用在PHP中使用files.Upload将文件上载到slack,php,slack,slack-api,Php,Slack,Slack Api,我正在尝试使用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 = n
<?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);
?>
更新
我将在这里发布完整的工作代码
<?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发送回复,并避免“超时已到”消息。缺少作用域。 您的令牌缺少必要的作用域,此处:
文件:write:user
要添加其他作用域,请执行以下操作:
假设您有一个Slack应用程序,请转到应用程序的管理页面,然后将其添加到“Ouath”子页面上。您可以在其中添加它。以下是指向您的应用程序的直接链接:
之后,您需要将应用程序重新安装到工作区,以便更改变为活动状态
使用卷发文件
另一个潜在的陷阱是CurlFile
显然只适用于文件的绝对路径。(). 快速修复方法如下所示:
$file=new CurlFile(realpath(dirname(uuu file_uuu)。'/../tmp/slack_icon.png'),'image/png')代码>欢迎!不幸的是,如果没有错误消息,我们将无法帮助您。如果出现空白屏幕,请尝试从页面末尾删除?>
,然后重试。确保你有。否则,我们不知道问题出在哪里。祝你好运@汤玛斯:你好!我已经打开了错误报告,但是如果我转到网页,仍然会看到一个空白屏幕。我还试图删除?>
,但是什么都没有。如果我通过slash命令运行脚本,我会从slack命令中收到一条空白消息,很明显您的令牌没有所需的作用域:您需要将文件:write:user
作用域添加到您的令牌token@ErikKalkoken我怎么做?我创建了一个机器人用户,现在我有了另一个OAuth令牌。使用这个新标记,现在我得到了这个{“ok”:false,“error”:“no_file_data”}
好的,现在我得到了这个{“ok”:true,“file”:{“id”:“FDZF1LH0R”,“created”:1541785891,“timestamp”:1541785891,“name”:“myIcon.jpg”…
我剪切了所有的字符串,因为它太长了,但它似乎在工作,但是如果我通过slack slash命令运行它,我会得到一个TimeoutCool。然后我会说最初的问题已经得到了回答。如何使用slash命令的3秒超时是一个完全不同的主题。这里已经回答了:我试图添加忽略用户_中止(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()
但它仍然给了我一个超时错误。但是上传工作正常,文件也显示在空闲状态。因此,这不仅仅是一个细节问题。我也从来没有让它与缓冲区捕获方法(ob_start(),…)一起工作过。但是请检查我链接的问题,看看基于curl的方法。这一方法非常有效。
<?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);
?>