Server : nginx/1.20.1 System : Linux iZ2ze9ojcl78uluczwag69Z 4.18.0-240.22.1.el8_3.x86_64 #1 SMP Thu Apr 8 19:01:30 UTC 2021 x86_64 User : www ( 1000) PHP Version : 7.3.28 Disable Function : passthru,exec,system,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv Directory : /www/wwwroot/0531yanglao.com/app/common/utils/ |
<?php namespace app\common\utils; use app\common\model\CmdLog; use app\common\exception\BaseException; use app\common\model\Log; use think\facade\Db; class YunpianSms { const VERIFY_TIME =300; const APIKEY = '6721174b96f607d7c7885b9d851c10a6'; /** * 发送短信 */ public static function sendSms($mobile , $code) { $data = array('tpl_id' => '4049966', 'tpl_value' => ('#code#'). '='.urlencode($code), 'apikey' => self::APIKEY, 'mobile' => $mobile); $json_data = self::tpl_send($data); if(false === $json_data) { exception('发送失败'); }else{ $r = json_decode($json_data,true); if($r['code'] == 0) { self::create_sms_log($mobile, $code, $json_data); return true; }else { Log::add($r,'yunpian_sms'); exception('发送失败'); } } } /** * 发送通知短信 * @param unknown $mobile 手机号 * @param unknown $tpl_id 模板ID key值 * @return boolean */ public static function sendNoticeSms($mobile ,$tpl_id) { if(!$mobile) return false; //$tpl_id = self::get_tpl_id($tpl_id); $tpl_id = intval($tpl_id); if(!$tpl_id) return false; $data = array('tpl_id' => $tpl_id,'apikey' => self::APIKEY, 'mobile' => $mobile); $json_data = self::tpl_send($data); if(false === $json_data) { return false; }else{ $r = json_decode($json_data,true); $r['tpl_id'] = $tpl_id; if($r['code'] == 0) { CmdLog::add($r,'yunpian_sms_tpl'); return true; }else { CmdLog::add($r,'yunpian_sms_tpl'); return false; } } } private static function tpl_send($data){ $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/tpl_single_send.json'); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); $result = curl_exec($ch); $error = curl_error($ch); if(false === $result) { CmdLog::add($error,'yunpian_sms_error'); return false; } return $result; } /** * 创建短信日志 * @param unknown $mobile * @param unknown $code * @param unknown $res */ private static function create_sms_log($mobile , $code , $res) { $data = [ 'mobile' => $mobile, 'code' => $code, 'ip' => request()->ip(), 'create_time' => time(), 'result_log' => $res, ]; Db::name('sms_log')->insert($data); } /** * 效验验证码是否有效 * @param unknown $mobile * @param unknown $code * @return boolean */ public static function verify($mobile ,$code) { $times=time()-self::VERIFY_TIME; $data=Db::name('sms_log')->where('mobile',$mobile)->where('code',$code)->where('create_time','>',$times)->find(); return $data ? true : false; } } /* 创建数据表 CREATE TABLE `aa_sms_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `mobile` varchar(20) NOT NULL, `code` varchar(10) NOT NULL, `ip` varchar(15) NOT NULL, `create_time` int(11) NOT NULL, `result_log` varchar(1000) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='短信日志'; // 调用示例: set_time_limit(0); header('Content-Type: text/plain; charset=utf-8'); $response = SmsDemo::sendSms(); echo "发送短信(sendSms)接口返回的结果:\n"; print_r($response); sleep(2); $response = SmsDemo::sendBatchSms(); echo "批量发送短信(sendBatchSms)接口返回的结果:\n"; print_r($response); sleep(2); $response = SmsDemo::querySendDetails(); echo "查询短信发送情况(querySendDetails)接口返回的结果:\n"; print_r($response); */