Last active
November 5, 2021 20:20
-
-
Save pereirawe/ebb407205c058adf301d47ef2c4e08a1 to your computer and use it in GitHub Desktop.
Slack Integration with PHP CURL
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 | |
/** | |
* OFFICIAL DOCUMENTATION | |
* https://github.com/php-slack/slack#sending-messages | |
* | |
* CRIAR WEBHOOK | |
* https://my.slack.com/services/new/incoming-webhook | |
* | |
* EDIT WEBHOOK | |
* https://my.slack.com/services/ | |
* | |
* ATTACHMENTS | |
* https://api.slack.com/messaging/composing/layouts#attachments | |
* */ | |
/** | |
* Send a Message to a Slack Channel | |
* @param string $message The message to post into a channel. | |
* @return boolean | |
*/ | |
function slack($message){ | |
$json = []; | |
$json['text'] = $message; | |
$json = json_encode($json); | |
$url = 'YOUR-SLACK-URL-TO-INTEGRATE'; | |
$ch = curl_init($url); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($json)) | |
); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
return $result; | |
} | |
$text = 'Hey there! ' . time() ; | |
if(isset($_GET['text'])){ | |
$text = $_GET['text']; | |
} | |
slack($text); | |
// END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment