Hyperf 阿里云短信

<?php

namespace App\Helper;

use App\Model\SmsSetting;

class Dysms
{
    const URL = 'http://dysmsapi.aliyuncs.com/?';
    const REGION_ID = 'cn-hangzhou';

    public function send(SmsSetting $setting, string $phone, int $code)
    {
        $accessKeyId = $setting->aliyun_access_key_id;
        $accessKeySecret = $setting->aliyun_access_secret;
        $signName = $setting->aliyun_sign_name;
        $templateCode = $setting->aliyun_template_id;

        $params = array(
            'Version' => '2017-05-25',
            'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
            'SignatureVersion' => '1.0',
            'SignatureNonce' => uniqid(),
            'SignatureMethod' => 'HMAC-SHA1',
            'Format' => 'JSON',
            'RegionId' => self::REGION_ID,
            'Action' => 'SendSms',
            'AccessKeyId' => $accessKeyId,
            'SignName' => $signName,
            'TemplateCode' => $templateCode,
            'PhoneNumbers' => $phone,
            'TemplateParam' => sprintf('{"code":%s}', $code),
        );

        $params =  $this->getSign($accessKeySecret, $params);

        return $this->request($params);
    }

    public function request(array $params)
    {
        $url = self::URL . http_build_query($params);

        $options = [
            'headers' => [
                'Content-type' => 'application/x-www-form-urlencoded',
            ],
        ];

        try {
            $result = make(Client::class)->request('GET', $url, [], $options)->getResult();

            $code = $result['Code'] ?? null;
            $message = $result['Message'] ?? null;

            if ($code !== 'OK') {
                Log::error(sprintf('阿里云短信发送失败[%s]:%s', $params['PhoneNumbers'], $message));
                return false;
            }
        } catch (\Exception $exception) {
            Log::error(sprintf('阿里云短信发送失败[%s]:%s', $params['PhoneNumbers'], $exception->getMessage()));
            return false;
        }

        return true;
    }

    private function getSign(string $accessKeySecret, array $params)
    {
        ksort($params);

        $canonicalizedQueryString = '';
        foreach ($params as $key => $value) {
            $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value);
        }

        $stringToSign = 'GET&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
        $signature = base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));
        $params['Signature'] = $signature;

        return $params;
    }

    private function percentEncode($string)
    {
        $string = urlencode($string);
        $string = preg_replace('/\+/', '%20', $string);
        $string = preg_replace('/\*/', '%2A', $string);
        $string = preg_replace('/%7E/', '~', $string);
        return $string;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

swoole~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值