-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_slack_message.php
32 lines (27 loc) · 1.23 KB
/
send_slack_message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
// Function to send a custom message to Slack webhook, utalized here for error and exception mesages, but can be used for pretty much anything
function sendToSlack($message,$emoji) {
$slackWebhookURL = ''; // Slack webhook URL that was created in slack admin
//takes the emoji and message passed as a parameter and stores it in a variable
$errorMessage = "$emoji $message";
$postData = array('text' => $errorMessage);
$jsonPayload = json_encode($postData);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $slackWebhookURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonPayload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($jsonPayload)));
// checks for a valid SSL certificate using the file downloaded as a reference
curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . '/cacert.pem');
$response = curl_exec($ch);
// Check for errors and success in cURL request / response and loggs them
if ($response === 'ok') {
return true;
} else {
return false;
}
// Close cURL resource
curl_close($ch);
}
?>