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;
	
	    }
	
	}
	
相关推荐
notillusion1 天前
URR#69598
java·php·程序优化
notillusion1 天前
CUK#22162
java·php·程序优化
张晓~183399481211 天前
碰一碰发视频 系统源码 /PHP 语言开发方案
开发语言·线性代数·矩阵·aigc·php·音视频·文心一言
qq_252924191 天前
PHP 8.0+ 现代Web开发实战指南 引
android·前端·php
普通网友1 天前
KUD#73019
java·php·程序优化
JaguarJack1 天前
PHP 桌面端框架NativePHP for Desktop v2 发布!
后端·php·laravel
普通网友1 天前
IZT#73193
java·php·程序优化
黄沐阳1 天前
stp,rstp,mstp的区别
服务器·网络·php
一念&1 天前
每日一个网络知识点:网络层NAT
服务器·网络·php