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;
	
	    }
	
	}
	
相关推荐
luanma15098043 分钟前
Laravel 10.x新特性全解析
php·laravel
Ynchen. ~6 小时前
告别 OSS 强制下载!阿里云 OSS 绑定自定义域名实现文件在线预览全记录
阿里云·云计算·oss
Irene19919 小时前
JavaScript脚本加载的两种方式:defer/async 的区别
前端·javascript·php
luanma1509809 小时前
PHP vs C#:30字秒懂两大语言核心差异
android·开发语言·python·php·laravel
fresh hacker10 小时前
【Linux系统】通用的“系统排障”
linux·运维·服务器·网络·php
luanma15098010 小时前
Laravel 8.X重磅特性全解析
前端·javascript·vue.js·php·lua
luanma15098010 小时前
Laravel 9.x重磅升级:十大新特性解析
php·laravel
zhangren0246810 小时前
PHP vs C++:从Web脚本到系统编程的终极对比
开发语言·c++·php
luanma15098010 小时前
Laravel 7.X核心特性深度解析
android·开发语言·php·lua·laravel
bug总结11 小时前
小程序云函数 vs 传统前后端分离(阿里云)原理解析
阿里云·小程序·云计算