VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • PHP发送短信

1.要拼接接收的手机号和短信

复制代码
 public function sendcode()
    {
        $parpm = input();
        $valist = $this->validate($parpm, [
            'phone' => 'require|regex:1[3-9]\d{9}'
        ]);
        if ($valist !== true) {
            $res = [
                'code' => 400,
                'msg' => $valist
            ];
            return Json($res);
            die;
        }
        $timer = cache('register_time_' . $parpm['phone']);
        if (time() - $timer < 60) {
            $rs = [
                'code' => '500',
                'msg' => '发送太过频繁'
            ];
            echo json_encode($rs);
            die;
        }
        $co = mt_rand(1000, 9999);
        $code = '%23code%23%3d' . $co;
        //发送短信验证
//          $resule=sendmsg($parpm['phone'],$code);
        $resule = true;
        if ($resule === true) {
            cache('register_code_' . $parpm['phone'], $co, 180);
            cache('register_time_' . $parpm['phone'], time(), 180);
            $re = [
                'code' => 200,
                'msg' => '短信发送成功',
                'data' => "$code"
            ];
            echo json_encode($re);
            die;
        } else {
            $re = [
                'code' => 400,
                'msg' => $resule
            ];
            echo json_encode($re);
            die;
        }
    }
复制代码

2.拼接要发送短信的要求

复制代码
if(!function_exists('sendmsg')){
     function sendmsg($phone,$code){
         $gateway=config('msg.gateway');
         $appkey=config('msg.appkey');
         $tpl_id=config('msg.tpl_id');
         $url=$gateway.'?mobile='.$phone.'&tpl_id='.$tpl_id.'&tpl_value='.$code.'&key='.$appkey;
         $res=curl_require($url,false,[],false);
         if(!$res){
            return '请求失败';
         }
         $arr=json_decode($res,true);
         if(isset($arr['code'])&&$arr['code']==10000){
              return true;
         }else{
             return false;
         }
     }
}
复制代码

3.向第三方发送请求

复制代码
if(!function_exists('curl_require')){
      function curl_require($url,$post=true,$parpm=[],$https=false){
          $ch=curl_init($url);
          if($post){
                curl_setopt($ch,CURLOPT_PORT,true);
                curl_setopt($ch,CURLOPT_POSTFIELDS,$parpm);
          }
          if($https){
               curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
          }
          curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
          $res=curl_exec($ch);
          curl_close($ch);
          return $res;
      }
}
复制代码


相关教程