Created
April 25, 2021 06:50
-
-
Save colinmurphy/86d44175fe2b491064fc78f1d4b5e049 to your computer and use it in GitHub Desktop.
Upload a file to Slack API with PHP Guzzle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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