阿里云短信(2022-09-01)

361次阅读
没有评论

阿里云2022年9月还可用的短信发送包。

 

<?php
namespace App\Repository;

class AlismsRepository{

    public function sms($call_number,$code,$canshu)
    {
        // $nihao = $this->sendSms('133********','{"code":"123456"}');
        $nihao = $this->sendSms($call_number,'{"'.$code.'":"'.$canshu.'"}');
        dd($nihao);
    }

    public $config=array('appId'=>'LTAI5tA4XNwdr8YoYKs8dM2R','appKey'=>'zQbtSxcMpCPEwDT9dfK3BbkfepFlTZ','tplId'=>'SMS_248860223','signName'=>'花田里鲜花店');
    /**
     * 发送短信
     * @param $mobile 手机号
     * @param $TemplateParam  str      '{"code":"1234"}'
     *
     * @return mixed
     */
    public function sendSms($mobile, $TemplateParam)
    {
        // 获取配置信息
        $config = $this->config;
        $params = array (
            'SignName' => $config['signName'],
            'Format' => 'JSON',
            'Version' => '2017-05-25',
            'AccessKeyId' => $config['appId'],
            'SignatureVersion' => '1.0',
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureNonce' => uniqid(),
            'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
            'Action' => 'SendSms',
            'TemplateCode' => $config['tplId'],
            'PhoneNumbers' => $mobile,
            // 营销短信无需 TemplateParam 参数
            'TemplateParam' => $TemplateParam //'{"code":"' . $code . '"}'
        );

        // 计算签名并把签名结果加入请求参数
        $params ['Signature'] = $this->computeSignature($params, $config['appKey']);

        // 发送请求
        $url = 'https://dysmsapi.aliyuncs.com/?' . http_build_query($params);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        $result = curl_exec($ch);
        curl_close($ch);
        $result=json_decode($result,true);
        return $result['Code']=='OK'?  true : false;
        //return $result;
    }

    /**
     * 短信签名计算
     * @param $parameters
     * @param $accessKeySecret
     *
     * @return string
     */
    protected function computeSignature($parameters, $accessKeySecret) {
        ksort($parameters);
        $canonicalizedQueryString = '';
        foreach ($parameters 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));
        return $signature;
    }

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

}

 

李路昌
版权声明:本站原创文章,由 李路昌 2022-09-01发表,共计2171字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)