Created
April 26, 2018 09:26
-
-
Save KaiserWerk/687220a64a9587b8aebea1dc11d7df77 to your computer and use it in GitHub Desktop.
Send a message to a slack channel with PHP
This file contains hidden or 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 | |
/** | |
* 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