Skip to content

Instantly share code, notes, and snippets.

@jenovateurs
Last active November 8, 2021 00:54
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 jenovateurs/e7d1d736c827f0363c162e3648e95023 to your computer and use it in GitHub Desktop.
Save jenovateurs/e7d1d736c827f0363c162e3648e95023 to your computer and use it in GitHub Desktop.
Send Message With Webhook Slack using PHP
<?php
public static function sendMessage($sMessage){
$webhookurl = 'https://hooks.slack.com/services/WEBHOOK';
$timestamp = date("c", strtotime("now"));
$json_data = json_encode([
// Message
"text" => $sMessage
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
$ch = curl_init( $webhookurl );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $json_data);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
// If you need to debug, or find out why you can't send message uncomment line below, and execute script.
// echo $response;
curl_close( $ch );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment