写在开头

原文请看教程修改,在此基础上根据官方最新文档编写了php版的封装代码

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,根据本代码添加了短信通知,效果如下

最后修改:2020 年 07 月 27 日 06 : 54 PM
赏包辣条吧