Skip to content

Instantly share code, notes, and snippets.

@disalvo
Forked from stefanzweifel/slack.php
Created August 31, 2018 11:31
Show Gist options
  • Save disalvo/eaabd23258a4efd04843ca0f9587bcbe to your computer and use it in GitHub Desktop.
Save disalvo/eaabd23258a4efd04843ca0f9587bcbe to your computer and use it in GitHub Desktop.
Slack.com Webhook Integration (PHP) - Simple snippet which tells you, how to build your payload array.
<?php
//Options
$token = 'YOUR_TOKEN_HERE';
$domain = 'YOUR_SLACK_DOMAIN_GOES_HERE';
$channel = '#general';
$bot_name = 'Webhook';
$icon = ':alien:';
$message = 'Your message';
$attachments = array([
'fallback' => 'Lorem ipsum',
'pretext' => 'Lorem ipsum',
'color' => '#ff6600',
'fields' => array(
[
'title' => 'Title',
'value' => 'Lorem ipsum',
'short' => true
],
[
'title' => 'Notes',
'value' => 'Lorem ipsum',
'short' => true
]
)
]);
$data = array(
'channel' => $channel,
'username' => $bot_name,
'text' => $message,
'icon_emoji' => $icon,
'attachments' => $attachments
);
$data_string = json_encode($data);
$ch = curl_init('https://'.$domain.'.slack.com/services/hooks/incoming-webhook?token='.$token);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
//Execute CURL
$result = curl_exec($ch);
return $result;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment