* @version : HUOSDK 8.0 * 短信发送API(SendSms)---PHP https://help.aliyun.com/document_detail/55451.html?spm=5176.10629532.106.2.1b471cbeRLbrO3 */ namespace huolib\sms\driver; use huolib\sms\SmsType; use huolib\status\MemberStatus; use think\Log; class Aliyun { protected $config = [ 'REGIONID' => 'cn-hangzhou', 'APPKEY' => '', 'APPSECRET' => '', 'PRODUCT' => '', 'SIGNNAME' => '', /*签名名称*/ 'SMSTEMPAUTH' => '', //身份验证验证码 'SMSTEMPTEST' => '', //短信测试 'SMSTEMPLOGIN' => '', //登录确认验证码 'SMSTEMPLOGINERROR' => '', //登录异常验证码 'SMSTEMPREG' => '', //用户注册验证码 'SMSTEMPACT' => '', //活动确认验证码 'SMSTEMPPWDMOD' => '', //修改密码验证码 'SMSTEMPINFOMOD' => '', //信息变更验证码 ]; public function __construct() { if (file_exists(GLOBAL_CONF_PATH."extra/sms/aliyun.php")) { $_config = include GLOBAL_CONF_PATH."extra/sms/aliyun.php"; } else { $_config = array(); } $this->config = array_merge($this->config, $_config); } /** * 标准返回 * * @param $code * * @return mixed */ private function getReturnByCode($code) { $_rdata['code'] = $code; $_rdata['msg'] = MemberStatus::getMsg($code); return $_rdata; } public function send($mobile, $type, $sms_code) { include_once EXTEND_PATH.'aliyun/aliyun-php-sdk-core/Config.php'; include_once EXTEND_PATH.'aliyun/Dysmsapi/Request/V20170525/SendSmsRequest.php'; include_once EXTEND_PATH.'aliyun/Dysmsapi/Request/V20170525/QuerySendDetailsRequest.php'; if (empty($this->config)) { Log::write("func=".__FUNCTION__."&class=".__CLASS__." config error", LOG::ERROR); return MemberStatus::CONFIG_ERROR; } $_profile = \DefaultProfile::getProfile( $this->config['REGIONID'], $this->config['APPKEY'], $this->config['APPSECRET'] ); switch ($type) { case SmsType::SMS_REG: { $_template_code = $this->config['SMSTEMPREG']; break; } case SmsType::SMS_LOGIN: { $_template_code = $this->config['SMSTEMPLOGIN']; break; } case SmsType::SMS_MODIFY: { $_template_code = $this->config['SMSTEMPPWDMOD']; break; } case SmsType::SMS_ID_VERIFY: { $_template_code = $this->config['SMSTEMPAUTH']; break; } case SmsType::SMS_FIND_PWD: { $_template_code = $this->config['SMSTEMPINFOMOD']; break; } case SmsType::SMS_BIND: { $_template_code = $this->config['SMSTEMPAUTH']; break; } case SmsType::SMS_OTHER: { $_template_code = $this->config['SMSTEMPACT']; break; } default: { $_template_code = $this->config['SMSTEMPAUTH']; break; } } \DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com"); $_acs_client = new \DefaultAcsClient($_profile); $_request = new \Dysmsapi\Request\V20170525\SendSmsRequest; //必填-短信接收号码 $_request->setPhoneNumbers($mobile); //必填-短信签名 $_request->setSignName($this->config['SIGNNAME']); //必填-短信模板Code $_request->setTemplateCode($this->config['SMSTEMPAUTH']); //选填-假如模板中存在变量需要替换则为必填(JSON格式) $_content = array( "code" => ''.$sms_code ); $_request->setTemplateParam(json_encode($_content)); //选填-发送短信流水号 $_request->setOutId("1234"); //发起访问请求 $_acsResponse = $_acs_client->getAcsResponse($_request); try { if ('OK' == $_acsResponse->Code) { return MemberStatus::NO_ERROR; } else { Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&mobile=".$mobile."&type=".$type."&code=" .$_acsResponse->Code ."&msg=".$_acsResponse->Code, LOG::ERROR ); if ('isv.BUSINESS_LIMIT_CONTROL' == $_acsResponse->Code) { return MemberStatus::PHONE_SEND_MORE; } return MemberStatus::PHONE_SEND_ERROR; } } catch (\ClientException $e) { Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&code=".MemberStatus::PHONE_SEND_ERROR ." ClientException error ".$e->getMessage(), LOG::ERROR ); return MemberStatus::PHONE_SEND_ERROR; } catch (\ServerException $e) { Log::write( "func=".__FUNCTION__."&class=".__CLASS__."&code=".MemberStatus::PHONE_SEND_ERROR ." ServerException error ".$e->getMessage(), LOG::ERROR ); return MemberStatus::PHONE_SEND_ERROR; } } }