//阿里云发送短信
public function send()
{
$phone=input('phone','');//手机号
if(empty($phone))
{
$this->BackResult('4', '手机号不能为空');
}elseif(!preg_match($this->phone,$phone))
{
$this->BackResult('5', '手机号格式不正确');
}
date_default_timezone_set("PRC");
$code=rand('100000','999999');
//发消息
$host = "https://zwp.market.alicloudapi.com";
$path = "/sms/sendv2";
$method = "GET";
$appcode = Config('app.AppCode');
$headers = array();
array_push($headers, "Authorization:APPCODE " . $appcode);
$content=urlencode('【IMSSVRD小帮手】您的动态密码为:'.$code.',您正在登录,若非本人操作,请勿泄露。');
$querys = "content={$content}&mobile={$phone}";
$bodys = "";
$url = $host . $path . "?" . $querys;
// echo $url;
// exit;
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
if (1 == strpos("$".$host, "https://"))
{
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$res=curl_exec($curl);
$res=json_decode($res,true);
// echo '<pre>';
// print_r($res);
if($res['error_code']==0)
{
//验证码加入redis
$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);
$redis->config("SET","notify-keyspace-events", "Ex");
$seconds =60*60*24;//过期时间60*15
$redis->setEx($phone,$seconds,$code);
$session=$redis->get($phone);
$this->BackResult('0', '发送成功',$session);
}
}
阿里云市场发送短信
最新推荐文章于 2022-08-17 16:49:37 发布
该代码段展示了如何使用阿里云服务发送短信验证码,并将其存储到Redis中以供后续验证。首先,检查手机号格式,然后生成随机验证码,接着通过GET请求调用阿里云API发送短信。如果发送成功,验证码会被存储到Redis中并设置过期时间。
摘要由CSDN通过智能技术生成