Skip to content

Instantly share code, notes, and snippets.

@pascalalich
Last active September 30, 2018 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pascalalich/5d0587127c5ee57c6912 to your computer and use it in GitHub Desktop.
Save pascalalich/5d0587127c5ee57c6912 to your computer and use it in GitHub Desktop.
Slack Notifications with PHP
<?php
$ch = curl_init();
// The Webhook URL from Slack's integration detail page
curl_setopt($ch, CURLOPT_URL, 'https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX');
// Only disable SSL checks like this for TESTING purposes if doesn't work otherwise. Make sure your PHP SSL certificates are up to date.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Build an HTTP POST instead of a GET request
curl_setopt($ch, CURLOPT_POST, 1);
// CURL will return the actual response as text, "ok" on success
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Fail verbosely if Slack returns an error, use for TESTING purposes
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
$payload = json_encode(array(
"username" => "Waste Seller GmbH",
"channel" => "#notifications",
"text" => "New waste entered"
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$result = curl_exec($ch);
curl_close($ch);
@pascalalich
Copy link
Author

Looks like this:

simple-slack-notification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment