1.打开腾讯云官网注册获取SDK AppID(appid).appkey(appkey).sign(签名内容).TemplateID(模板id);

2. composer 安装腾讯云的依赖

    在项目 composer.json 文件里添加   "qcloudsms/qcloudsms_php": "0.1.*" 段代码   然后在项目根目录下执行composer  update 命令。

3.

use Qcloud\Sms\SmsSingleSender;

public function registr( Request $api )
		{
			$mobile = $api->input( 'mobile' );
			$count = rand( 000000 , 999999 );  //生成六位随机数
			Cache::set( 'users' , $count , 5 );  //使用cache 缓存六位随机数 时间为五分钟
			$appid = config( 'teng.Appid' );
			$appkey = config( 'teng.appkey' );
			$sign = config( 'teng.sign' );
			$TemplateID = config( 'teng.TemplateID' );
			$res = self::msender( $appid , $appkey , $mobile , $count , $TemplateID , $sign );
			$res = json_decode( $res );
			if ( $res->result == 0 ) {
				return [
					'code'  => 101,
					'msg'   => '发送成功'
				];
			} else {
				return [
					'code'  => 101,
					'msg'   => $res->errmsg
				];
			}
		}




public function msender(
			$appid ,
			$appkey ,
			$phoneNumbers ,
			$code ,
			$templateId ,
			$sign
		) {
			try {
				$ssender = new SmsSingleSender( $appid , $appkey );
				$params = [ $code ];
				$result = $ssender->sendWithParam( "86" , $phoneNumbers , $templateId ,
					$params , $sign , "" , "" );  // 签名参数未提供或者为空时,会使用默认签名发送短信
				$rsp = json_decode( $result );
				return $result;
			} catch ( \Exception $e ) {
				return $e;
			}
		}

4. 手机接收短信。

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐