Loading... ## 写在开头 <div class="tip inlineBlock info"> 原文请看[教程][1]修改,在此基础上根据官方最新文档编写了php版的封装代码 </div> ## php代码 ``` <?php //不是post直接返回 header('Content-Type: text/html;charset=utf-8'); if ($_SERVER["REQUEST_METHOD"] != "POST") { die('<script>alert("别闹(。");location.href="./";</script>'); } define('SecretId','你的SecretId'); define('SecrSetKey','你的secretKey'); define('PolicyId','告警策略'); function sms_send($text, $desp = '') { $param = array( 'Action' => 'SendCustomAlarmMsg', 'Region' => 'ap-shanghai', 'Version' => '2018-07-24', 'Module' => 'monitor', 'PolicyId' => PolicyId, 'Timestamp' => time(), 'Nonce' => rand(0, 65535), 'SecretId' => SecretId, "Msg"=>$text."\n".$desp, ); ksort($param); $signStr = "GETmonitor.tencentcloudapi.com/?"; foreach ($param as $key => $value) { $signStr = $signStr . $key . "=" . $value . "&"; } $signStr = substr($signStr, 0, -1); $signature = base64_encode(hash_hmac("sha1", $signStr,SecretKey, true)); return $result = file_get_contents('https://monitor.tencentcloudapi.com/?' . http_build_query($param).'&Signature='.urlencode($signature)); } header('Content-Type: application/json;charset=utf-8'); $text = ""; $desp = ""; $_POST = $_POST ? $_POST : json_decode(file_get_contents('php://input'), true); //获取post参数 if (isset($_POST["text"])) { $text = $_POST["text"]; } if (empty($text)) { $json['errno'] = 1; $json['errmsg'] = "消息标题不能为空啦"; die(json_encode($json)); } if (isset($_POST["desp"])) { $desp = $_POST["desp"]; } echo sms_send($text, $desp); ``` ## 用法 向上面的php文件发送post请求 参数: text:消息标题 desp:消息内容 ## 效果 我修改了下valine-admin,根据本代码添加了短信通知,效果如下 <img width = "80%" height = "80%" src="https://cdn.jsdelivr.net/gh/iLay1678/images/imgs/TIM图片20200401164204.jpg"/> [1]: https://blog.craftyun.cn/post/165.html 最后修改:2020 年 07 月 27 日 06 : 54 PM © 允许规范转载 赞赏 赏包辣条吧 ×Close 赞赏作者 扫一扫支付 支付宝支付 微信支付