Skip to content

Instantly share code, notes, and snippets.

@mghayour
Created April 8, 2017 21:45
Show Gist options
  • Save mghayour/7adbf7bf953df89c3ba2f8b22c1b82c8 to your computer and use it in GitHub Desktop.
Save mghayour/7adbf7bf953df89c3ba2f8b22c1b82c8 to your computer and use it in GitHub Desktop.
this php file report php fatal errors to your telegram account
<?php
/*
this php file report php fatal errors to your telegram account
setup :
1- search for "//TODO: edit this" and edit that !
2- include it before all
3- enjoy debuging fast as possible
*/
function shutdown_report_it() {
$error = error_get_last();
if ($error['type'] === E_ERROR) {
echo("<p>ERRR</p>");
// fatal error has occured
$err="🚨🚨🚨🚨🚨🚨\n";
$err.=json_encode($error);
$err.="\nπŸ†˜πŸ†˜πŸ†˜πŸ†˜πŸ†˜πŸ†˜\n";
reportErrorWithTelegram($err);
}
}
register_shutdown_function('shutdown_report_it');
function report_system_error($errno, $errstr, $errfile, $errline)
{
if (!$errno) {
// This error code is not included in error_reporting
return;
}
$msg='';
switch ($errno) {
case E_USER_ERROR:
case E_ERROR:
case E_COMPILE_ERROR:
$msg.= "<b>My ERROR</b> [$errno]\n $errstr\n";
$msg.= "πŸ†˜FatalπŸ†˜ πŸ†˜errorπŸ†˜ on line $errline in file $errfile";
$msg.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")\n";
$msg.= "Aborting...<br />\n";
reportErrorWithTelegram($msg);
exit(1);
break;
default:
$msg.= "Unknown error type: [$errno]\n $errstr\n";
$msg.= " ❌error❌ on line $errline in file $errfile";
$msg.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")\n";
reportErrorWithTelegram($msg);
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// User a custom error handler to print output
set_error_handler( 'report_system_error' );
//report error with telegram
function reportErrorWithTelegram($msg) {
// echo ("reportErrorWithTelegram runned");
// fatal error has occured
//TODO: edit this
$tid=90054900; // user id number in telegram (its mine, @GhayourForEver)
$reply = array('chat_id'=>$tid,'text'=>$msg,'disable_web_page_preview'=>true);
$sended_msg = tele_req_with_crash_bot('sendMessage',$reply);
// do not run app again until bug solve ...
// it have bug, is this really neeed ??
// file_put_contents("./__die.php",'<?php die("APP CRASHED, WAIT FOR DEBUGING"); ? >');
}
function tele_req_with_crash_bot ($method,$data) { // telegram request
//mghayourbot
$API_KEY = 'YOUR_BOT_TOKEN'; //TODO: edit this
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.telegram.org/bot'.$API_KEY.'/'.$method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);
//$result=utf8_decode($result);
$result=json_decode($result,true);
if ($result['ok'])
return $result['result'];
else
{
// dvar($result);
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment