Slack Bot To Handle PHP Errors Real Time

As a full-stack developer, I was facing a big problem to keep eye on all errors happening in the PHP codes. Always I need to look into the error log file in the server to find out what is issue happened. If that is a Big Project I can’t figure out where and what is the issue always. It always eats my lot of time to 1. Find the bug 2. Where the bug took place. When It takes place. After brainstorming, I figure out why I can’t detect the error and send it to slack in real-time. So I started researching it and found a good solution. And here is the Step By Step Procedure to Do that.

You Can Hire Toptal Developers for Developing Slack Apps

https://www.toptal.com/slack

Step 1

Create a Slack channel Example Name “#server-error-production”

Step 2

Go to https://api.slack.com/apps and Click On Create New App

Put Your App Name and select the team.

After Create You will Get Of Basic Information. Click On Incoming Webhooks

Activate Incoming Webhooks Click on “Off” Toggler Button

Click On “Add New Webhook To Team”

This will open a window to authorize to post the channel, Select the channel “server-error-production” and Click on Authorize.

Click On Copy and Save this url for future Use.

Step 3

Create error_handler_slack.php on the server. Eg: /home/example_project/public_html/error_handler_slack.php

<?php
$error_reporting = 'On'; // On or Off
if($error_reporting == 'On')
{
set_error_handler('common_error_handler');
register_shutdown_function("shutdown_error_handler");
}
function shutdown_error_handler() //will be called when php script ends.
{
$lasterror = error_get_last();
if(isset($lasterror))
{
$message = '{"text": "Error ('.$lasterror['type'].') | PHP Stopped | Message ('.$lasterror['message'].') | File ('.$lasterror['file'].')"}';
$ch = curl_init("[YOUR-SLACK-CHANNEL-HOOK-URL]");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);
}
}
function common_error_handler($number, $message, $file, $line, $vars)
{
$message = '{"text": "An error ('.$number.') occurred on line '.$line.' and in the file: '.$file.'."}';
// echo $message;
$ch = curl_init("[YOUR-SLACK-CHANNEL-HOOK-URL]");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $message);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
curl_close($ch);

//return $result;

// Make sure that you decide how to respond to errors (on the user's side)
// Either echo an error message, or kill the entire project. Up to you...
// The code below ensures that we only "die" if the error was more than
// just a NOTICE.
if ( ($number !== E_NOTICE) && ($number < 2048) ) {
die("There was an error. Please try again later.");
}
}
?>

In the above File You Need to replace [YOUR-SLACK-CHANNEL-HOOK-URL] with you URL that is copied from the step 2.

Step 4.

Include this error_handler_slack.php on the PHP script you need to track.

Whenever your PHP script fails you will get a live notification to your slack Instantly.

If you a lazy coder and you need to include this to all php script automatically.

Find auto_prepend_file in php.ini and paste the path of the script

auto_prepend_file /home/example_project/public_html/error_handler_slack.php

OR Add the below code in .htaccess

php_value auto_prepend_file /home/example_project/public_html/error_handler_slack.php

If any question reach me @ https://m.me/rajeshjnair

You Can Hire Toptal Developers for Developing Slack Apps

https://www.toptal.com/slack

Tech Enthusiast | Entrepreneur | Chatbot Specialist 🤖 | Text/Speech AI Explorer | Almost Down Earth Coder

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store