tp5.1发送阿里云短信验证码

使用tp模拟生成手机号,并发送阿里云短信验证码

1.项目根目录执行composer,安装sdk

composer require alibabacloud/client

2.发送验证码示例

php 复制代码
// 公共文件
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;   

// 代码如下
    private static $mobileSegment = [
	        '134', '135', '136', '137', '138', '139', '150', '151', '152', '157', '130', '131', '132', '155', '186', '133', '153', '189',
	    ];
	
	public function nextMobile()
	{
        // 模拟生成手机号
		$prefix = self::$mobileSegment[array_rand(self::$mobileSegment)];
		$middle = mt_rand(2000, 9000);
		$suffix = mt_rand(2000, 9000);

        $mobile = $prefix . $middle . $suffix;

		
		
		$code = mt_rand(2000, 9000);//这里是随机生成4位数字
		
		$res    = self::smsVerify($mobile, $code, '你的template_code');
    
		if($res['status'] == 1){
		
		    dump(1,"验证码已发送");
		
		}else{
		
		    dump(0,"验证码发送失败,请联系客服");
		
		}
	}
	
	
	/**
	
	  * 验证码(阿里云短信)
	
	  */
	
	function smsVerify($mobile, $code, $tempId)
	
	{     
	
	   AlibabaCloud::accessKeyClient('你的access_key_id', '你的access_key_secret')
	
	                        ->regionId('cn-hangzhou') //replace regionId as you need(这个地方是发短信的节点,默认即可,或者换成你想要的)
	
	                        ->asGlobalClient();
	
	    $data = [];
	
	    try {
	
	        $result = AlibabaCloud::rpcRequest()
	
	                  ->product('Dysmsapi')
	
	                  //->scheme('https') //https | http(如果域名是https,这里记得开启)
	
	                  ->version('2017-05-25')
	
	                  ->action('SendSms')
	
	                  ->method('POST')
	
	                  ->options([
	
	                        'query'                 => [
	
	                            'PhoneNumbers'      => $mobile,
	
	                            'SignName'          => '你的sign_name',
	
	                            'TemplateCode'      => $tempId,
	
	                            'TemplateParam'     => json_encode(['code'=>$code]),
	
	                        ],
	
	                    ])
	
	                  ->request();
	
	        $res    = $result->toArray();
	        if($res['Code'] == 'OK'){
	            $data['status'] = 1;
	            $data['info']   = $res['Message'];
	        }else{
	            $data['status'] = 0;
	            $data['info']   = $res['Message'];	
	        }

	        return $data;
	
	    } catch (ClientException $e) {
	        $data['status'] = 0;
	        $data['info']   = $e->getErrorMessage();
	        return $data;
	
	    } catch (ServerException $e) {
	        $data['status'] = 0;
	        $data['info']   = $e->getErrorMessage();
	        return $data;
	
	    }
	
	}
	
相关推荐
Hi2024021742 分钟前
基于阿里云ECS搭建Tailscale DERP中继服务器:提升跨网络连接速度
服务器·阿里云·云计算
名誉寒冰5 小时前
TCP, 三次握手, 四次挥手, 滑动窗口, 快速重传, 拥塞控制, 半连接队列, RST, SYN, ACK
网络·tcp/ip·php
m0_738120725 小时前
CTFshow系列——PHP特性Web97-100
开发语言·安全·web安全·php·ctfshow
~央千澈~8 小时前
【01】针对开源收银系统icepos (宝塔面板) 详细安装教程详细参考-优雅草卓伊凡
开源·php
Hi202402171 天前
基于阿里云部署 RustDesk 自托管服务器
运维·服务器·阿里云·云计算·远程控制·远程桌面
I like Code?1 天前
阿里云服务器配置ssl-docker nginx
服务器·阿里云·ssl
前端世界1 天前
Python 正则表达式实战:用 Match 对象轻松解析拼接数据流
python·正则表达式·php
苏琢玉1 天前
用 PHP 玩向量数据库:一个从小说网站开始的小尝试
php·composer
wuk9981 天前
ThinkPHP 6框架常见错误:htmlentities()函数参数类型问题解决
php
xrkhy1 天前
阿里云对象存储OSS的使用
java·数据库·阿里云