Created
January 11, 2019 09:22
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 | |
/** | |
* Send a Message to a Slack Channel. | |
* | |
* Get the API Token visit: https://api.slack.com/custom-integrations/legacy-tokens | |
* The token will look like this `xoxo-250000090-0000000000-0000000000-dbssg`. | |
* | |
* @param string $message The message to post in channel. | |
* @param string $channel The name of the channel prefixed with # | |
* @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, | |
"text" => $message, | |
"username" => "SlackBot", | |
]); | |
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; | |
} | |
slack('Hello World', '#your-slack-channel'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment