php钉钉机器人推送消息api

2022-01-27 19:51:45 浏览数 (1)

最近内测那个QQHOO!K机器人挺火的,突发奇想写个小玩意直接提交很香,上手的时候tx大大不知因为个啥不给玩了,钉钉有个webhook,于是就有了下边这个玩应

随手写的,代码又臭又长希望有大佬给指正下

代码语言:javascript复制
PHP复制PHP复制PHP复制    <?php
    /**
     * php 钉钉推送dome
     * User: 神奇的七云
     * Date: 2020/4/15
     * Time: 15:04
     */
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
    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);
        //$info = curl_getinfo($ch);
        //var_dump($info);
        curl_close($ch);
        return $data;
    }
    $webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx";
    /**
     * @var mixed $content  正文
     * @var mixed $title  标题
     * @var mixed $picUrl 图片链接
     * @var mixed $messageUrl 跳转链接
     */
    $time = date("Y-m-d H:i:s", time());
    $content = ($_GET['content']);
    $title = ($_GET['title']);
    $picUrl = ($_GET['picUrl']);
    $messageUrl = ($_GET['messageUrl']);
    $h1 = $_GET['h1'];
    if ($key == md5(md5(date("Ymdhis", time()) - 66547997 * "66547997"))) {
        if ($_GET['type'] == 'text') {
            // text类型 
            //?type=text&content=正文
            $textString = json_encode([
                'msgtype' => 'text',
                'text' => [
                    "content" => $content
                ],
            ]);
            $result = request_by_curl($webhook, $textString);
            echo $result;
        } elseif ($_GET['type'] == 'link') {
            // link类型
            //?type=text&content=正文&title=标题&picUrl=图片链接&messageUrl跳转链接
            $textString = json_encode([
                "msgtype" => "link",
                "link" => [
                    "text" => $content,
                    "title" => $title,
                    "picUrl" => $picUrl,
                    "messageUrl" => $messageUrl,
                ]
            ]);
            $result = request_by_curl($webhook, $textString);
            echo $result;
        } elseif ($_GET['type'] == "markdown") {
            //markdown类型
            //?type=text&content=正文&title=标题&picUrl=图片链接&messageUrl跳转链接
            $textString = json_encode([
                "msgtype" => "markdown",
                "markdown" => [
                    "title" => $title,
                    "text" => "#### $title,n" .
                        "> $contentnn" .
                        "> ![screenshot]($picUrl)n" .
                        "> ###### $time 发布 n"
                ],
                "at" => [
                    "atMobiles" => [
                        "156xxxx8827",
                        "189xxxx8325"
                    ],
                    "isAtAll" => false
                ]
            ]);
            $result = request_by_curl($webhook, $textString);
            echo $result;
        } elseif ($_GET['type'] == "ActionCard") {
            // 整体跳转ActionCard类型
            //?type=text&content=正文&title=标题&picUrl=图片链接&messageUrl跳转链接&h1=一级标题
            $textString = json_encode([
                "actionCard" => [
                    "title" => $title,
                    "text" => "![]($picUrl) 
    ### $h1 
    $content",
                    "hideAvatar" => "0",
                    "btnOrientation" => "0",
                    "singleTitle" => "阅读全文",
                    "singleURL" => "$messageUrl"
                ],
                "msgtype" => "actionCard"
            ]);
            $result = request_by_curl($webhook, $textString);
            echo $result;
        } else {
            $result = request_by_curl($webhook, $textString);
            echo $result;
        }
    }

0 人点赞