PHP试题网_中国最大的免费网络PHP试题测试平台,PHP试卷调查,PHP试卷模板、PHP考试测验
公告: 20191230公告

接收阿里云直播报警并将报警信息发送到企业微信群


<?php
namespace app\h5\controller;

use think\Controller;
use think\Db;

class Alert extends Controller
{
    public function receive()
    {
        //$data_org = input('post.');
        $data_org = file_get_contents('php://input');
        //dd($data_org);
        if(empty($data_org))
        {
            exit('fail');
        }

        $id = Db::connect('mall_log')->name('alert')->insertGetId(['act'=>'live_alert', 'msg'=> $data_org, 'create_time'=>time()]);

        echo 'success';
        fastcgi_finish_request();

        $json     = json_decode($data_org, true);
        if(!is_array($json) || empty($json)  )
        {
            return ;
        }

        if(empty($json['StreamName']) || empty($json['OssObject'])  || empty($json['OssBucket'])  ||  empty($json['Result'])   )
        {
            return ;
        }

        $ret = [
            'logid'         => $id,
            'StreamName'    => $json['StreamName'],
            'OssObject'     => '在阿里云申请的域名/'.$json['OssObject'],
            'OssBucket'     => $json['OssBucket'],
        ];
        foreach($json['Result'] as $k => $v)
        {
            foreach($v['Result'] as $k2 => $v2)
            {
                $msg = $Suggestion = '';
                switch ($v2['Label'])
                {
                    case 'sexy':
                        $msg = '性感';
                        break;
                    case 'porn':
                        $msg = '鉴黄';
                        break;
                    case 'terrorism':
                        $msg = '暴恐';
                        break;
                    case 'politics':
                        $msg = '政治';
                        break;
                }
                $Suggestion = $v2['Suggestion'];
                if(!empty($msg))
                {
                    break;
                }
            }
            if(!empty($msg))
            {
                break;
            }
        }

        $ret['msg']        = $msg ?? '';
        $ret['Suggestion'] = $Suggestion ?? '';

        if(!empty($ret['msg']))
        {
            $this->send_wxwork($ret);
        }
    }


    private function send_wxwork($ret)
    {
        $url   = '手机企业微信建的群,有一个机器人,点进去。会有一个url,复制放到这里来';
        $atwho = ['131xxxxxxxx3','@all'];

        $ret['StreamName'] = str_ireplace('stream_', '', $ret['StreamName']);
        $data  = [
            'msgtype' => 'text',
            'text'    => [
                'content'               => '直播房间['.$ret['StreamName'].']疑似['.$ret['msg'].']直播,建议['.$ret['Suggestion'].'],图片文件名['.$ret['OssObject'].'],OssBucket['.$ret['OssBucket'].'],本次报警日志id['.$ret['logid'].'],请各位大佬过目!',
                'mentioned_mobile_list' => $atwho,
            ],
        ];
        $header  = ['Content-Type: application/json'];

        $datastr = json_encode($data);
        $tmpret  = $this->curl_($url, $datastr, 10, $header);

        echo ($tmpret);
        return ;
    }

    //用于测试
    public function test()
    {
        $ret = [
            'logid' => 2333,
            'StreamName' => 'stream_441',
            'OssObject'  => 'abc.jpg',
            'OssBucket'  => 'mybucket',
            'msg'        => '封',
            'Suggestion' => '阻止'
        ];
        //$this->send_wxwork($ret);
    }

    private function curl_($url, $data, $timeout, $header)
    {
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在

        if($header) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //发送header请求
        }
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求

        if($data) {
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); // 设置超时限制 防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        $err     = '';
        if(curl_errno($curl)) {
            $err = 'Err:' . curl_error($curl); // 捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        if($err) {
            $tmpInfo = $err . $tmpInfo;
        }
        return $tmpInfo; // 返回数据
    }
}


效果:

image.png


也有同事说,可以直接发图片到此报警群内呀,这样直接一点。我觉得群内有女同事,对于不适合的图片,建议不要直接发到群给让女同事看到,免得造成骚扰。还是发文字好些。


作者:OK兄 浏览次数:5

登 录