业务规则>短信模板内容审核标准 * @var string $TEMPLATE_PARAS */ $TEMPLATE_PARAS = '["'.$code.'"]'; //模板变量,根据自身使用的模板,其值长度和个数与模板对应 //请求Headers $headers = [ 'Content-Type: application/x-www-form-urlencoded', 'Authorization: WSSE realm="SDP",profile="UsernameToken",type="Appkey"', 'X-WSSE: '.buildWsseHeader($APP_KEY, $APP_SECRET) ]; //请求Body $data = http_build_query([ 'from' => $sender, 'to' => $receiver, 'templateId' => $TEMPLATE_ID, 'templateParas' => $TEMPLATE_PARAS, 'statusCallback' => $statusCallback, 'signature' => $signature //使用国内短信通用模板时,必须填写签名名称 ]); $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10); $response = curl_exec($ch); return $response; } /** * 构造X-WSSE参数值 * @param string $appKey * @param string $appSecret * @return string */ function buildWsseHeader($appKey, $appSecret) { date_default_timezone_set('Asia/Shanghai'); $now = date('Y-m-d\TH:i:s\Z'); //Created $nonce = uniqid(); //Nonce $base64 = base64_encode(hash('sha256', ($nonce . $now . $appSecret))); //PasswordDigest return sprintf("UsernameToken Username=\"%s\",PasswordDigest=\"%s\",Nonce=\"%s\",Created=\"%s\"", $appKey, $base64, $nonce, $now); } /**手机号验证 * @param $mobile * @return bool */ function checkMobile($mobile){ if (!is_numeric($mobile)) { return false; } return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false; } /**邮箱验证 * @param $email * @return bool */ function checkEmail($email){ if (!$email) { return false; } return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false; } /** * @param * @return int */ function makeSalt(){ $salt = rand(10000000,99999999); return $salt; } /** * @param $account * @param float|int $expire * @return string */ function makeToken($account,$expire=0){ $tokeninfo = Db::table("sys_token")->where(['accountid'=>$account['id']])->find(); $date = date("Y-m-d H:i:s"); $token = sha1($account['username'].$account['mobile'].$date); if(!$tokeninfo){ $data=['token'=>$token,"accountid"=>$account['id'],"addtime"=>$date,"expire"=>$expire]; }else{ $data=$tokeninfo; $data['token']=$token; $data['addtime']=$date; $data['expire']=$expire; } $resulr=Db::table("sys_token")->save($data); return $resulr? $token:""; } /** * @param $token * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ function VerifyToken($token){ $tokeninfo = Db::table("sys_token")->where(['token'=>$token])->find(); if(!$tokeninfo){ return ['code'=>101,"message"=>'token不存在']; } if($tokeninfo['expire']102,"message"=>'token已失效']; } $acc = Db::name("view_userinfo")->where("id","=",$tokeninfo['accountid'])->find(); if(!$acc){ return ['code'=>103,"message"=>'未找到对应的账户']; } $verify=sha1($acc['username'].$acc['mobile'].$tokeninfo['addtime']); if($verify!=$token){ return ['code'=>104,"message"=>'token无效']; } $tokeninfo['expire'] = time()+1800; Db::table("sys_token")->save($tokeninfo); return ['code'=>0,"message"=>'验证通过',"user"=>['id'=>$tokeninfo['accountid']]]; } /** * POST 请求 * @param string $url * @param array $param * @param boolean $post_file 是否文件上传 * @return string content */ function post($url,$data,$header=[]) { //对空格进行转义 $url = str_replace(' ','+',$url); $ch = curl_init(); //设置选项,包括URL curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch,CURLOPT_TIMEOUT,3); //定义超时3秒钟 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // POST数据 // curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, 1); // 把post的变量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //所需传的数组用http_bulid_query()函数处理一下,就ok了 curl_setopt($ch, CURLOPT_HEADER, true); //执行并获取url地址的内容 $output = curl_exec($ch); $errorCode = curl_errno($ch); //释放curl句柄 $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); // 根据头大小去获取头信息内容 $header = substr($output, 0, $headerSize); curl_close($ch); if(0 !== $errorCode) { return false; } return $header; } function post2($url,$data,$header=[]) { //对空格进行转义 $url = str_replace(' ','+',$url); $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //设置选项,包括URL curl_setopt($ch, CURLOPT_URL, "$url"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // POST数据 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_POST, 1); // 把post的变量加上 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //所需传的数组用http_bulid_query()函数处理一下,就ok了 $output = curl_exec($ch); $errorCode = curl_errno($ch); curl_close($ch); if(0 !== $errorCode) { return false; } return $output; }