首页
开发手册
应用中心
工具
用户中心
Develop Your Own FoundPHP Library
English
中文简体
English
上一篇
下一篇
### Develop Your Own FoundPHP Library FoundPHP is an extremely mini core library. If we want to develop our own library, it can be easily extended. You can also develop your own excellent open source library by following the system's standard specifications. #### PHP Core Library | path | | | description | | ------------ | ------------ | ------------ | ---------- - | | plugin | | | | | | class | | **system class library** | | | | sms |
**SMS verification code class**
| #### SMS Verification Code Library Development In order to make the library you develop more authentic, we use the actual sms library to join the FoundPHP library. We need to create a `sms` directory under the `plugin/class/` directory, and create a php file in utf8 format, named `sms.php`. Class library structure: Class name: FoundPHP_sms Sending method: send() Log method: logs() Called SMS sending interface Tencent SMS platform SDK reference: https://github.com/TencentCloud/tencentcloud-sdk-php manual reference: https://cloud.tencent.com/document/product/382/43070 class library file plugin/class/sms/sms.php ``` id = $ary['id']; $this->nationcode = empty($ary['code'])?'86':$ary['code']; //omestic sector $this->key = $ary['key']; $this->sign = $ary['sign']; $this->logs_dir = $ary['logs_dir']; } /* send a message phone receive information mobile phone txt text message content tplid template id */ function send($phone='',$txt='',$tplid=''){ $logs = 'Date:'.dates(time())."\r\n"; $logs .= 'Phone:'.$phone."\r\n"; $logs .= 'Msg:'.$txt."\r\n"; $logs .= 'TPLid:'.$tplid."\r\n"; $logs .= 'Sign:'.$this->sign."\r\n"; $data = new \stdClass();//create an empty object without member methods and properties $tel = new \stdClass(); $tel->nationcode = "86"; $tel->mobile = "".$phone; $data->tel = $tel; $nowtime = time(); $random = rand(100000, 999999);//generate random numbers $data->sig =hash("sha256", "appkey=".$this->key."&random=".$random."&time=".$nowtime."&mobile=".$phone);// generate signature $data->tpl_id = $tplid; $data->params = array($txt); $data->sign = $this->sign; $data->time = $nowtime; $data->extend = ''; $data->ext = ''; //request $res = json($GLOBALS['tpl']->curl("https://yun.tim.qq.com/v5/tlssmssvr/sendsms". "?sdkappid=" . $this->id . "&random=" . $random,$data,1)); if($res['result']==0){ $logs .= 'Info:'.$res['errmsg']."\r\n---------------------------\r\n"; $this->logs($logs); return json_out(1,array('msg'=>$res['errmsg']),1); }else{ $logs .= 'Code:'.$res['result']."\r\n"; $logs .= 'Info:'.$res['errmsg']."\r\n---------------------------\r\n"; $this->logs($logs); return json_out($res['result'],array('msg'=>$res['errmsg']),1); } } //Log function logs($txt=''){ if (is_dir($this->logs_dir) && $this->logs_dir==true){ $GLOBALS['tpl']->writer($this->logs_dir.'/'.dates(time(),$this->filename).'_sms.txt',$txt,'a+'); } } } ``` ------------ **Call Method:** plugin/model/default.php ``` send('19102021188',$rand_code,$tpl_id)); if ($res['code']==1){ echo 'success'; } ?> ``` The class library and use encapsulated here are complete. Fill in the real mobile phone number, the verification code uses a random number, and the template id uses the real one to send the SMS, and the verification code will be received in a few seconds.