Thinkphp5.X异常接管后通过钉钉机器人推送通知
大多团队在生产环境下都会关闭app_debug,所以相对应的错误信息就不能实时查看到。以下分享一个目前团队项目正在用的生产环境下相关技术人员能第一时间获取通知的方法:
先上效果图:
以上通知是在钉钉中显示!
下面介绍开发步骤:
1:Thinkphp版本需在5.X或以上,在config/app.php中接管异常处理
(个人案例中调用方式,具体命名依照对应项目建立)
'exception_handle' => '\\app\\Common\\Controller\\Exception',
具体操作可以参考官方文档:https://www.kancloud.cn/manual/thinkphp5_1/354092
2:在异常接管方法中实现以下代码
<?php
/*
* 异常接管
*/
namespace app\Common\Controller;
use Exception;
use think\exception\Handle;
use think\exception\HttpException;
use think\exception\ValidateException;
class Exception extends Handle {
public function render(Exception $e) {
if (method_exists($e, 'getStatusCode')) {
// 参数验证错误
if ($e instanceof ValidateException) {
return json($e->getError(), 422);
}
// 请求异常
if ($e instanceof HttpException && request()->isAjax()) {
return response($e->getMessage(), $e->getStatusCode());
}
if ($e->getStatusCode() == 404) {
$module = \think\facade\Request::module();
!$module and $module = 'index';
return view($module . '@public/404');
}
if ($e->getStatusCode() == 500) {
return parent::render($e);
}
} else {
$file = $e->getFile();
$line = $e->getLine();
$code = parent::getCode($e);
$message = parent::getMessage($e);
$error_message = '[' . $code . '] ErrorException in ' . $file . ' line ' . $line . PHP_EOL . $message;
// 此处为钉钉server
$server = new \app\Server\DingdingServer();
$data = [
'text' => ['content' => $error_message]
];
$server->robotSend($data);
}
// 其他错误交给系统处理
return parent::render($e);
}
}3:钉钉Server的创建
<?php
/**
* 钉钉开放API
* https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
*/
namespace app\Server;
class DingdingServer {
private $API_URL = "https://oapi.dingtalk.com/robot/send?access_token=xxxxx";
public function __construct() {
}
/**
*
* @param array $data
* @param string $msgtype text link markdown actionCard
* @return boolean
*/
public function robotSend($data = [], $msgtype = 'text') {
if (empty($data)) {
return false;
}
$final_data = $data + ['msgtype' => $msgtype];
$data_string = json_encode($final_data);
$webhook = $this->API_URL;
$result = $this->request_by_curl($webhook, $data_string);
return $result;
}
protected function request_by_curl($remote_server, $post_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_server);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json;
charset = utf-8'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
// curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
// curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}此处钉钉接口需要一个access_token参数,具体操作步骤参考:
1:先下载PC版钉钉
2:发起群聊,添加至少2人创建群聊
3:点击群聊界面右上角三个点中的 群机器人,选择 添加机器人,选择最后一个 自定义
4:钉钉API地址就在webhook中
到这里,生产环境下即使关闭了app_debug,我们也能第一时间收到异常记录了!特别提醒,钉钉貌似每分钟只能发送20条推送。
关于钉钉的API,代码中默认调用text文本形式,其实还有很多,例如 link markdown actionCard 方式,小伙伴们可以自行研究~
声明:版权所有,违者必究 | 如未注明,均为原创 | 本网站采用 BY-NC-SA 协议进行授权
转载:转载请注明原文链接,违者必究 - :https://www.wolfcode.net/info/129/


