Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MikSDigital/be489a72802822e746d9da77e42a1a43 to your computer and use it in GitHub Desktop.
Save MikSDigital/be489a72802822e746d9da77e42a1a43 to your computer and use it in GitHub Desktop.
Post a message to a slack channel with PHP
<?php
/**
* Send a Message to a Slack Channel.
*
* In order to get the API Token visit: https://api.slack.com/custom-integrations/legacy-tokens
* The token will look something like this `xoxo-2100000415-0000000000-0000000000-ab1ab1`.
*
* @param string $message The message to post into a channel.
* @param string $channel The name of the channel prefixed with #, example #foobar
* @return boolean
*/
function slack($message, $channel)
{
$ch = curl_init("https://slack.com/api/chat.postMessage");
$data = http_build_query([
"token" => "YOUR_API_TOKEN",
"channel" => $channel, //"#mychannel",
"text" => $message, //"Hello, Foo-Bar channel message.",
"username" => "MySlackBot",
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// Example message will post "Hello world" into the random channel
slack('Hello world', '#random');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment