//设置关键字 测试环境 $url = 'https://oapi.dingtalk.com/robot/send?access_token=bbb034d110e485fa9ec457b5446eab2270778c93100752d609750c425223868b'; $msg = '测试环境测试消息'; try{ new cat(); }catch (Throwable $e){ $result = [ "> 错误信息:" .addslashes($e->getMessage()), "> 错误文件:" . addslashes($e->getFile()), "> 错误行号:" . addslashes($e->getLine()), "> ###### 日期:" . date('Y-m-d H:i:s'), ]; send_dingtalk_err($url,join(' \n', $result)); } /* * 发送叮叮机器人错误消息 */ function send_dingtalk_err($url,$msg) { send_dingtalk($url, '错误通知(' . '测试环境' . ')', '测试环境'.$msg); } /* * 发送叮叮机器人消息 */ function send_dingtalk($url, $title, $text, $at = '', $all = 'true') { try { http_post($url, '{"msgtype":"markdown","markdown":{"title":"' . $title . '","text":"#### ' . $title . ' \n' . $text . '"},"at":{"atMobiles":"' . $at . '","isAtAll":' . $all . '}}'); } catch (Exception $e) { } } function http_post($url, $json, $timeout = 120, &$error = null) { return curl_request($url, $json, 'POST', $timeout, ['Content-Type: application/json', 'Content-Length: ' . strlen($json)], false, $error); } /* * 发送后请求 * curl_request($url, $json, 'POST', 30, array('Content-Type: application/json', 'Content-Length: ' . strlen($json)), false,$error) * curl_request($url, $data, 'POST', 30, null, false, $error) * curl_request($url, null, 'GET', 30, null, false, $error) */ function curl_request($url, $vars, $method = "GET", $timeout = 120, $header = null, $ssl = false, &$error = null) { $curl = curl_init($url); curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $ssl); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $ssl); if (strtoupper($method) == "POST") { curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $vars); } else { curl_setopt($curl, CURLOPT_POST, false); } if (!is_null($header)) { curl_setopt($curl, CURLOPT_HTTPHEADER, $header); } $data = curl_exec($curl); if ($data === false) { $error = curl_errno($curl); curl_close($curl); return false; } else { curl_close($curl); var_dump($data); return $data; } }
php使用钉钉机器人消息
最新推荐文章于 2024-06-15 12:02:39 发布