Skip to content

Instantly share code, notes, and snippets.

@alexstone
Created March 3, 2014 06:54
Show Gist options
  • Save alexstone/9319715 to your computer and use it in GitHub Desktop.
Save alexstone/9319715 to your computer and use it in GitHub Desktop.
Fire a Slack Notification via CURL
<?php
// (string) $message - message to be passed to Slack
// (string) $room - room in which to write the message, too
// (string) $icon - You can set up custom emoji icons to use with each message
public static function slack($message, $room = "engineering", $icon = ":longbox:") {
$room = ($room) ? $room : "engineering";
$data = "payload=" . json_encode(array(
"channel" => "#{$room}",
"text" => $message,
"icon_emoji" => $icon
));
// You can get your webhook endpoint from your Slack settings
$ch = curl_init("WEBHOOK ENDPOINT GOES HERE");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
// Laravel-specific log writing method
// Log::info("Sent to Slack: " . $message, array('context' => 'Notifications'));
return $result;
}
@InternetMedicineMan
Copy link

How are you able to set the channel? When I'm giving permission for my app to use the Incoming Webhook, I get a unique URL for every single Channel. I can send the channel all day long, but it's the URL that defines which channel it goes to. Is there a generic URL that I can't find? Or is there a permission I need to create that I am overlooking?

@mosin771098
Copy link

mosin771098 commented Jun 21, 2017

hello
i want to use slack rest api in curl to create a channel not with webhook but that is not working plz help me

@netEmmanuel
Copy link

Those this still work i am getting an error missing_text_or_fallback_or_attachments

@raholiarivelo
Copy link

thanks, it's help me so mush :)

in symfony nexylan / slack-bundle, we use "withIcon" to change the icon but here how to do it ?

@dani3l3
Copy link

dani3l3 commented Jan 31, 2021

Brilliant, thanks. emoji icons aren't working for me but, besides that, it's a very useful function!

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