Skip to content

Instantly share code, notes, and snippets.

@KaiserWerk
Created April 26, 2018 09:26
Show Gist options
  • Save KaiserWerk/687220a64a9587b8aebea1dc11d7df77 to your computer and use it in GitHub Desktop.
Save KaiserWerk/687220a64a9587b8aebea1dc11d7df77 to your computer and use it in GitHub Desktop.
Send a message to a slack channel with PHP
<?php
/**
* Sends a Message to a Slack Channel.
*
* @param string $message
* @param string $channel
* @return string A json string
*/
function sendSlackMessage($message, $channel)
{
$ch = curl_init("https://slack.com/api/chat.postMessage");
$data = http_build_query(array(
"token" => "YOUR_APIKEY",
"channel" => $channel, // format: "#channel"
"text" => $message,
"username" => "SlackTestBot",
));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// dump the json result
var_dump(sendSlackMessage('Hello world', '#test'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment