阿里云短信发送代码封装

忽见某一高手为了实现发送短信,竟然引入了整个aliyunsdk,我一阵语塞。。。

在这里分享一个项目中调用阿里云短信发送的PHP实现吧。

注意:下面的类中 namespaceconfig 均为伪代码,请自行替换。

/**
 * 阿里云短信
 * @package rehiy\util
 * @author 若海<https://www.rehiy.com>
 */

namespace rehiy\util;

use rehiy\Config;
use rehiy\Exception;

class AliyunSms
{
    protected $accessKeyId = '';
    protected $accessKeySecret = '';

    protected $signName = '';
    protected $templateCode = '';

    protected $apiUrl = 'http://dysmsapi.aliyuncs.com/';

    public function __construct()
    {
        $this->accessKeyId = Config::get('sms_appid');
        $this->accessKeySecret = Config::get('sms_secret');

        $this->signName = Config::get('sms_signame');
        $this->templateCode = Config::get('sms_template_code');
    }

    public function send(string $phone, array $data)
    {
        $params = [
            'AccessKeyId' => $this->accessKeyId,
            'Action' => 'SendSms',
            'Format' => 'JSON',
            'PhoneNumbers' => $phone,
            'RegionId' => 'cn-hangzhou',
            'SignName' => $this->signName,
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => uniqid(mt_rand(0, 0xffff), true),
            'SignatureVersion' => '1.0',
            'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
            'Version' => '2017-05-25',
            'TemplateCode' => $this->templateCode,
            'TemplateParam' => json_encode($data, JSON_UNESCAPED_UNICODE),
        ];

        ksort($params);
        $sortedQuery = http_build_query($params, '', '&', PHP_QUERY_RFC3986);

        $signature = "POST&%2F&" . rawurlencode($sortedQuery);
        $signature = rawurlencode(
            base64_encode(hash_hmac('sha1', $signature, $this->accessKeySecret . '&', true))
        );

        $body = "Signature={$signature}&{$sortedQuery}";
        $header = ['x-sdk-client' => 'php/2.0.0'];

        return $this->httpRequest('POST', $this->apiUrl, $body, $header);
    }

    private function httpRequest(string $method, string $url, $data = null, $header = [])
    {
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_TIMEOUT, 25);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

        if (!empty($header)) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        }

        if ($data && is_array($data)) {
            $data = http_build_query($data);
        }
        if ($method == 'POST' && $data) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }

        list($body, $errno, $error) = [
            curl_exec($ch), curl_errno($ch), curl_error($ch), curl_close($ch)
        ];

        if ($errno != 0) {
            throw new Exception('CURL - ' . $error, $errno);
        }

        try {
            return json_decode($body, true);
        } catch (Exception $e) {
            return false;
        }
    }
}

AlpineLinux 升级v3.14到v3.15

重要提示: 新版系统的 sshd 禁用了 ssh-rsa 密钥,如果你使用了此类密钥登录,请一定要开启 ssh-rsa 认证方式。

#!/bin/sh
#

# 更新系统版本

sed -i 's/3.14/3.15/g' /etc/apk/repositories

# 更新系统软件

apk update

apk add --upgrade apk-tools
apk upgrade --available && sync

# 修改 motd 内容

echo "Welcome to Alpine!" >/etc/motd

# 升级 sshd 配置

if [ -f /etc/ssh/sshd_config.apk-new ]; then
    rm -f /etc/ssh/sshd_config
    mv /etc/ssh/sshd_config.apk-new /etc/ssh/sshd_config
    echo "" >>/etc/ssh/sshd_config
    echo "Port 2200" >>/etc/ssh/sshd_config
    echo "PubkeyAcceptedKeyTypes=+ssh-rsa" >>/etc/ssh/sshd_config
fi

# 升级 mkinitfs 配置

if [ -f /etc/mkinitfs/mkinitfs.conf.apk-new ]; then
    rm -f /etc/mkinitfs/mkinitfs.conf
    mv /etc/mkinitfs/mkinitfs.conf.apk-new /etc/mkinitfs/mkinitfs.conf
fi

# 清理已知冗余配置

rm -f \
    /etc/update-extlinux.conf.apk-new \
    /etc/shadow.apk-new \
    /etc/fstab.apk-new \
    /etc/inittab.apk-new \
    /etc/motd.apk-new \
    /etc/passwd.apk-new \
    /etc/fstab.apk-new \
    /etc/hostname.apk-new \
    /etc/group.apk-new \
    /etc/hosts.apk-new \
    /etc/conf.d/loadkmap.apk-new \
    /etc/conf.d/node-exporter.apk-new

# 查看其他未处理的配置

find /etc/ -name *-new

key type ssh-rsa not in PubkeyAcceptedAlgorithms

全新安装 AlpinLinux-v3.15.0,配置好之前使用的密钥。

竟然无法登录 ??错误信息: key type ssh-rsa not in PubkeyAcceptedAlgorithms

竟然不支持 ssh-rsa。这也太搞笑了吧,翻阅 OpenSSH 文档后,终于顺利解决。

echo "PubkeyAcceptedKeyTypes=+ssh-rsa" >>/etc/ssh/sshd_config