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;
    }
}

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

总结

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

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大力水手z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值