php CI框架异常报错通过钉钉自定义机器人发送

php CI框架异常报错通过钉钉自定义机器人发送


文章目录


前言

我们在项目开发中,经常会遇到自己测试接口没问题,上线之后就会测出各种问题,主要是因为我们开发的时候测试频率少了,接口参数都是合法,上线之后用户多了,各种参数,各种请求上来就会出现一些BUG,这时候我们不主动查看日志很难发现,所以收到需求需要把项目的所有抛出异常信息发送到钉钉


一、封装一个异常监测

这里因为我们使用的php CI框架,我列出我的代码

复制代码
class MY_Exceptions extends CI_Exceptions
{
    public function __construct()
    {
        parent::__construct();
        require_once APPPATH . 'libraries/DingDing.php';
    }

    public function show_exception($exception)
    {
        $errorMessage = $exception->getMessage();
        $errorFile = $exception->getFile();
        $errorLine = $exception->getLine();
        $errorTrace = $exception->getTraceAsString();

        // 构建要发送的错误信息字符串
        $errorDetails = "Error occurred in file $errorFile at line $errorLine:\n\n";
        $errorDetails .= "Error message: $errorMessage\n\n";
        $errorDetails .= "Stack trace:\n$errorTrace";
        DingDing::send_msg($errorDetails);
        parent::show_exception($exception);
    }
}

这里主要目的就是通过监测CI框架自己抛出的异常,我们在列出我们需要的异常信息,报错文件,代码行数,拼接成我们需要的信息之后再发送到钉钉,让我们好定位问题去解决

二、封装好钉钉信息发送

代码如下(示例):

复制代码
class DingDing
{
    const Sign_key = "xxxxxxxxxxxxxxxxxxxxxx";

    const Web_hook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxx";

    public static function send_msg($msg)
    {
        $times = time() * 1000;
        $sign_key = self::Sign_key;
        $sign = urlencode(base64_encode(hash_hmac('sha256', $times . "\n" . $sign_key, $sign_key, true)));
        $webhook_url = self::Web_hook . "&timestamp={$times}&sign={$sign}";
        $data = array('msgtype' => 'text', 'text' => array('content' => $msg));
        $data_string = json_encode($data);
        $result = self::posturl($webhook_url, $data_string);
    }

    public static function posturl($url , $data) {
        //启动一个CURL会话
        $ch = curl_init();

        // 设置curl允许执行的最长秒数
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
        // 获取的信息以文件流的形式返回,而不是直接输出。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

        //发送一个常规的POST请求。
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        //要传送的所有数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($data))
        );

        // 执行操作
        $res = curl_exec($ch);
        /*echo '>>>>>>';
        var_dump($res);
        echo '<<<<<<';*/
        if ($res == NULL) {
            curl_close($ch);
            return false;
        } else if(curl_getinfo($ch, CURLINFO_HTTP_CODE) != "200") {
            curl_close($ch);
            return false;
        }

        curl_close($ch);
        return $res;
    }
}

钉钉机器人的相关参数自己去创建申请,然后所有功能就完成了

总结

文章主要用到框架自带的异常抛出,和钉钉机器人发送消息的使用功能,代码大家对比自己项目修改就可以直接使用了,希望能帮到你

相关推荐
还鮟2 小时前
CTF Web PHP弱类型与进制绕过(过滤)
php·ctf
zorro_z2 小时前
PHP语法基础篇(八):超全局变量
php
黄雪超2 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice2 小时前
对象的finalization机制Test
java·开发语言·jvm
思则变3 小时前
[Pytest] [Part 2]增加 log功能
开发语言·python·pytest
lijingguang3 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#
¥-oriented3 小时前
【C#中路径相关的概念】
开发语言·c#
CoderCodingNo3 小时前
【GESP】C++四级考试大纲知识点梳理, (7) 排序算法基本概念
开发语言·c++·排序算法
青牛科技-Allen4 小时前
GC3910S:一款高性能双通道直流电机驱动芯片
stm32·单片机·嵌入式硬件·机器人·医疗器械·水泵、
恋猫de小郭4 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin