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;
	
	    }
	
	}
	
相关推荐
知行产研2 小时前
中国矿山无人驾驶出海的三重门
数据库·mysql·php
前进的程序员4 小时前
告别Copilot?Codex本地化部署指南
开源·php·copilot·codex
weixin_BYSJ19874 小时前
springboot智慧公共交通系统---附源码51123
java·javascript·spring boot·python·django·flask·php
2401_861678625 小时前
阿里云远程连接
阿里云·云计算
迪康coolmu5 小时前
企业文件外发防泄漏实践:从通道封堵到三层全链路管控
大数据·运维·网络·人工智能·安全·阿里云
郑州光合科技余经理20 小时前
海外版多语言团购系统架构:主数据互通与核销边界
java·开发语言·前端·后端·系统架构·php·ai编程
heimeiyingwang1 天前
【架构实战】Kubernetes网络模型深度解析:从Pod通信到Ingress网关实战
开发语言·架构·php
运维行者_1 天前
预测性云监控怎么做?AI驱动的7大核心能力与落地路径
服务器·开发语言·网络·数据库·人工智能·python·php
eggrall1 天前
Socket编程UDP
网络协议·udp·php
牛马也想出海2 天前
亚马逊爬虫代理IP:IP类型选择与配置指南
开发语言·php