Skip to content

Instantly share code, notes, and snippets.

@pereirawe
Last active November 5, 2021 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pereirawe/ebb407205c058adf301d47ef2c4e08a1 to your computer and use it in GitHub Desktop.
Save pereirawe/ebb407205c058adf301d47ef2c4e08a1 to your computer and use it in GitHub Desktop.
Slack Integration with PHP CURL
<?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