安装阿里云短信客服端
准备工作: 申请阿里云短信签名、模板以及获取阿里云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;
}