短信服务(Short Message Service)是阿里云为用户提供的一种通信服务的能力,支持快速发送短信验证码、短信通知等,服务范围覆盖全球。 完美支撑双11期间2亿用户,发送6亿短信。三网合一专属通道,与工信部携号转网平台实时互联。电信级运维保障,实时监控自动切换,到达率高达99%。
thinkphp5整合阿里大于短信,很遗憾的是阿里大于不支持composer,只能手动下载demo安装了!demo下载地址,下载好的demo放在vendor下
将阿里大于配置信息写在配置文件中config.php
'alipay_smg' => [ 'product' => 'Dysmsapi', 'domain' => 'dysmsapi.aliyuncs.com', 'accessKeyId' => '', 'accessKeySecret' => '', 'region' => 'cn-hangzhou', 'endPointName' => 'cn-hangzhou', 'setTemplateCode' => '', 'setSignName' => '' ],
直接贴代码,在application下建了Server.php
<?php /** * User: jung * Date: 2018/6/20 * Time: 15:50 */ namespace app\server; require_once VENDOR_PATH .'/aliyun-dysms-php-sdk/api_sdk/vendor/autoload.php'; use Aliyun\Core\Profile\DefaultProfile; use Aliyun\Core\DefaultAcsClient; use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest; use Aliyun\Core\Regions\EndpointConfig; use think\Config; define('ENABLE_HTTP_PROXY', FALSE); define('HTTP_PROXY_IP', '127.0.0.1'); define('HTTP_PROXY_PORT', '8888'); class Sms{ static $acsClient = null; static $config; public static function getAcsClient() { if(static::$acsClient == null) { $profile = DefaultProfile::getProfile(self::$config['region'], self::$config['accessKeyId'], self::$config['accessKeySecret']); EndpointConfig::load(); //这里要注意一下,引入是新增的 DefaultProfile::addEndpoint(self::$config['endPointName'], self::$config['region'], self::$config['product'], self::$config['domain']); static::$acsClient = new DefaultAcsClient($profile); } return static::$acsClient; } public static function initialize($config = []){ if(!self::$config){ self::$config = \think\Config::get('alipay_smg'); } return self::$config; } public static function getCode($length = 4){ if($length < 4){ return false; } $min = '1'; $max = '9'; for ($i = 1 ; $i < $length ;$i++){ $min .= '0'; $max .= '9'; } return rand(intval($min),intval($max)); } /** * @param $mobile * @return bool * @throws \Exception * 发送短信验证码 */ public static function sendSms($mobile) { if(empty($mobile)){ throw new \Exception('手机号码不能为空'); } if(!preg_match('/^0?(13|14|15|17|18|16)[0-9]{9}$/', $mobile)){ throw new \Exception('不正确的手机号码'); } self::initialize(); $request = new SendSmsRequest(); $request->setPhoneNumbers($mobile); $request->setSignName(self::$config['setSignName']); $request->setTemplateCode(self::$config['setTemplateCode']); $code = self::getCode(); $request->setTemplateParam(json_encode([ "code"=>$code, "product"=>"dsd" ])); $acsResponse = static::getAcsClient()->getAcsResponse($request); if('OK' == $acsResponse->Message && 'OK' == $acsResponse->Code){ return true; } throw new \Exception($acsResponse->Message,0); } }
具体发送之后处理逻辑就自己处理下咯!