阿里大于 阿里云 短信

安装阿里云短信客服端

准备工作: 申请阿里云短信签名、模板以及获取阿里云AccessKey ID和AccessKey Secret。
框架: TP6.0

composer require alibabacloud/client

发送短信

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
/**
 * @Notes 发送短信
 * @Author shenz
 * @Date 2020-06-11 15:40
 * @param $mobile 发送手机号
 * @param $code 模板ID
 * @param $data 短信模板变量
 */
public function sendSms($mobile, $code,$data){
	#获取阿里云KEY ID Secret等配置
	$accessKeyId = Config::getConfigParame("aliyun_access_key_id");
	$accessSecret = Config::getConfigParame("aliyun_access_key_secret");
	$autograph = Config::getConfigParame("aliyun_sms_autograph");
	//配置ID和密钥
	AlibabaCloud::accessKeyClient($accessKeyId, $accessSecret)
	    ->regionId('cn-hangzhou')
	    ->asDefaultClient();
	try {
	    //发送短信
	    $result = AlibabaCloud::rpc()
	        ->product('Dysmsapi')
	        // ->scheme('https') // https | http
	        ->version('2017-05-25')
	        ->action('SendSms')
	        ->method('POST')
	        ->host('dysmsapi.aliyuncs.com')
	        ->options([
	            'query' => [
	                'RegionId' => "cn-hangzhou",
	                'PhoneNumbers' => $mobile,//发送手机号
	                'SignName' => $autograph,//签名
	                'TemplateCode' => $code,//模板ID
	                'TemplateParam' => json_encode($data),//模板替换变量
	            ],
	        ])
	        ->request();
	    //json转数组
	    $result = $result->toArray();
	    //判断是否发送成功
	    if(strtolower($result["Code"]) == "ok" && strtolower($result["Message"]) == "ok"){
	        return true;
	    }
	} catch (ClientException $e) {
	//            echo $e->getErrorMessage() . PHP_EOL;
	} catch (ServerException $e) {
	//            echo $e->getErrorMessage() . PHP_EOL;
	}
	return false;
}

附上一篇阻止刷短信捣乱的代码

/**
     * @Notes 判断是否可以发送短息
     * @Author shenz
     * @Date 2020-04-29 15:38
     * @param string $mobile
     * @param string $temp_code
     * @return bool|string
     */
    public static function isSendSms($mobile="",$temp_code=""){
        #获取记录 判断当前手机号是否可以发送短信验证码
        $isSend = self::where("mobile",$mobile)->where("expir",">",time())->value("expir",0);
        #判断是否可以继续发送
        if($isSend){
            return ($isSend - time())."秒后可继续发送";
        }
        #获取一个手机号一个小时段内的发送次数
        $count = self::where("mobile",$mobile)->where("create_time",">",time()-3600)->count("id");
        #判断是否超额
        if($count >= 5){
            return "同一个手机号一个小时内发送次数不能超过五次";
        }
        #获取IP
        $ip = app()->request->ip();
        #获取一个IP一个小时内的发送次数
        $count = self::where("ip",$ip)->where("create_time",">",time()-3600)->count("id");
        #判断是否超额
        if($count >= 5){
            return "同一个IP一个小时内发送次数不能超过五次,可关闭WIFI重试";
        }
        #获取一个手机号同一个短信模板一天内的发送次数
        $count = self::where("mobile",$mobile)->where("template_code",$temp_code)->where("create_time",">",strtotime(date("Y-m-d",time())))->count("id");
        #判断是否超额
        if($count >= 5){
            return "同一个手机号相同短信一天内发送次数不能超过五次";
        }
        #获取一个IP一天内的发送次数
        $count = self::where("ip",$ip)->where("create_time",">",strtotime(date("Y-m-d",time())))->count("id");
        #判断是否超额
        if($count >= 10){
            return "同一个IP一天内发送次数不能超过十次,可关闭WIFI重试";
        }
        #获取同一个手机号一天内的发送次数
        $count = self::where("mobile",$mobile)->where("create_time",">",strtotime(date("Y-m-d",time())))->count("id");
        #判断是否超额
        if($count >= 10){
            return "同一个手机号一天内的发送次数不能超过十次";
        }
        #获取一个小时内的发送次数
        $count = self::where("create_time",">",time()-3600)->count("id");
        #判断是否超额
        if($count >= 500){
            return "当前发送短信已超额,请一个小时后重试";
        }
        return true;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值