Skip to content

Instantly share code, notes, and snippets.

@colinmurphy
Created April 25, 2021 06:50
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinmurphy/86d44175fe2b491064fc78f1d4b5e049 to your computer and use it in GitHub Desktop.
Save colinmurphy/86d44175fe2b491064fc78f1d4b5e049 to your computer and use it in GitHub Desktop.
Upload a file to Slack API with PHP Guzzle
<?php
require_once "../vendor/autoload.php";
use GuzzleHttp\Client;
/**
* Notes:
*
* Tested with guzzlehttp/guzzle version 7.3
* See https://api.slack.com/methods/files.upload for details on how to generate a token
*
*/
$fileName = '';
$filePath = '';
$slacktoken = ''; // See https://api.slack.com/tokens; The token will need file.read and file.write permissions
$client = new Client();
$apiUrl = 'https://slack.com/api/files.upload';
$client = new Client();
$request = $client->post( $apiUrl, [
'headers' => ['Authorization' => 'auth_trusted_header'],
'multipart' => [
[
'name' => 'token',
'contents' => $slacktoken,
],
[
'name' => 'file',
'contents' => fopen($filePath, 'r'),
'filename' => $fileName
],
[
'name' => 'channels',
'contents' => '#general'
],
[
'name' => 'initial_comment',
'contents' => 'File Uploaded'
]
]
]);
var_dump($request->getBody()->getContents());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment