网建短信通接口

2019-05-28  本文已影响0人  敲代码的小仙女

网建短信通相比较阿里云要方便很多,不需要企业认证,签名和模板也不需要审核,注册完成之后就可以使用了。

官网地址

http://www.smschinese.cn/

短信类
<?php
/**
 * 手机短信类
 */

namespace sendmsg;
use think\Db;

class Sms
{
    /*
     * 发送手机短信
     * @param unknown $mobile 手机号
     * @param unknown $content 短信内容
    */
    public function send($mobile, $content)
    {
        return $this->mysend_sms($mobile, $content);
    }

    /*
    您于{$send_time}绑定手机号,验证码是:{$verify_code}。【{$site_name}】
    -1  没有该用户账户
    -2  接口密钥不正确 [查看密钥]不是账户登陆密码
    -21 MD5接口密钥加密不正确
    -3  短信数量不足
    -11 该用户被禁用
    -14 短信内容出现非法字符
    -4  手机号格式不正确
    -41 手机号码为空
    -42 短信内容为空
    -51 短信签名格式不正确接口签名格式为:【签名内容】
    -6  IP限制
   大于0 短信发送数量
    http://utf8.api.smschinese.cn/?Uid=本站用户名&Key=接口安全秘钥&smsMob=手机号码&smsText=验证码:8888
    */
    private function mysend_sms($mobile, $content)
    {
        $user_id = urlencode(config('smscf_wj_username')); // 这里填写用户名
        $key = urlencode(config('smscf_wj_key')); // 这里填接口安全密钥
        if (!$mobile || !$content || !$user_id || !$key)
            return false;
        if (is_array($mobile)) {
            $mobile = implode(",", $mobile);
        }
        $mobile=urlencode($mobile);
        $content=urlencode($content);
        $url = "http://utf8.api.smschinese.cn/?Uid=" . $user_id . "&Key=" . $key . "&smsMob=" . $mobile . "&smsText=" . $content;
        if (function_exists('file_get_contents')) {
            $res = file_get_contents($url);
        }
        else {
            $ch = curl_init();
            $timeout = 5;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $res = curl_exec($ch);
            curl_close($ch);
        }
        if ($res >0) {
            return true;
        }
        return false;
    }
}
获取短信验证码
   /**
     * 获取短信验证码
     * @param $input
     * @return mixed
     */
    public static function getCheckSms($input)
    {
        header("Content-Type: text/html;charset=utf-8");
        $sms_mobile = $input['mobile'];
        $sms_captcha = rand(100000, 999999);
        $log_msg = '您好!您的验证码是'.$sms_captcha.',若非本人操作,请忽略。';

        $recordData['mobile']       = $input["mobile"];
        $recordData['type']         = 1;
        $recordData['message']      = $log_msg;
        $recordData['create_time']  = date('Y-m-d H:i:s');
        $sms_id = Db::table('sms_record')->insertGetId($recordData);

        $result = self::sendSms($sms_mobile,$log_msg,1,$sms_captcha);
        if($result){
            // 短信记录更新
            $recordDataUpdate['result_status'] = $result['state'] ? 0 : 1;
            $recordDataUpdate['result_info']  = '';
            $recordDataUpdate['result']  = json_encode($result);
            $recordDataUpdate['update_time']  = date('Y-m-d H:i:s');
            Db::table('sms_record')->where('id', $sms_id)->update($recordDataUpdate);
            if($result['state']){
                cache($sms_mobile."_code",$sms_captcha,600);
                $res['msg'] = '短信发送成功';
                return $res;
            }else{
                $res['error'] = $result['message'];
                return $res;
            }
        }
    }
验证短信是否可以发送
    /**
     * 发送验证码
     * @author csdeshang
     * @param type $smslog_phone 手机号
     * @param type $smslog_msg 短信
     * @param type $smslog_type 类型
     * @param type $smslog_captcha 验证码
     * @param type $member_id 会员ID
     * @param type $member_name 会员名
     * @return type
     */
    public static function sendSms($smslog_phone,$smslog_msg,$smslog_type='',$smslog_captcha='')
    {

        //通过手机号判断是否允许发送短信
        $begin_add_time = strtotime(date('Y-m-d'));
        $end_add_time = strtotime(date('Y-m-d')) + 24 * 3600;

        //同一IP 每天只能发送20条短信
        $condition = array();
        $condition['smslog_ip'] = request()->ip();
        $condition['create_time'] = array('between', array($begin_add_time, $end_add_time));
        if (self::getSmsCount($condition) > 20) {
            return array('state'=>FALSE,'code'=>10001,'message'=>'同一IP地址一天内只能发送20条短信,请勿多次获取动态码!');
        }

        //同一手机号,60秒才能提交发送一次
        $condition = array();
        $condition['mobile'] = $smslog_phone;
        $condition['create_time'] = array('between', array(TIMESTAMP-30, TIMESTAMP));
        if (self::getSmsCount($condition) > 0) {
            return array('state'=>FALSE,'code'=>10001,'message'=>'同一手机30秒后才能再次发送短信,请勿多次获取动态码!');
        }

        //同一手机号,每天只能发送5条短信
        $condition = array();
        $condition['mobile'] = $smslog_phone;
        $condition['create_time'] = array('between', array($begin_add_time, $end_add_time));
        if (self::getSmsCount($condition) > 5) {
            return array('state'=>FALSE,'code'=>10001,'message'=>'同一手机一天内只能发送5条短信,请勿多次获取动态码!');
        }

        $sms = new \sendmsg\Sms();
        $result = $sms->send($smslog_phone, $smslog_msg);

        if ($result) {

            $data["mobile"] = $smslog_phone;
            $data["create_time"] = time();
            $data['smslog_ip'] = request()->ip();
            $data["status"] = 1;
            $data["remark"] = $smslog_captcha;

            $return = Db::table("sms_code")->insert($data);

            if($result>=0){
                return array('state'=>TRUE,'code'=>10000,'message'=>'');
            }else{
                return array('state'=>FALSE,'code'=>10001,'message'=>'手机短信发送失败');
            }
        }else{
            return array('state'=>FALSE,'code'=>10001,'message'=>'手机短信发送失败');
        }
    }
获取短信条数
    /**
     * 获取数据条数
     * @access public
     * @author csdeshang
     * @param type $condition 条件
     * @return type
     */
    public static function getSmsCount($condition) {
        return Db::table('sms_code')->where($condition)->count();
    }
短信记录数据库表

sms_code


1559023549(1).png
短信验证
配置
    'sms_overtime'      => 600,
    'sms_overcount'     => 5,
验证
    /**
     * 验证短信验证码
     * @param $mobile 手机号
     * @param $code 验证码
     */
    public static function checkSmsCode($mobile, $code)
    {
        $map["mobile"] = $mobile;

        $arrSms = Db::table('sms_code')->field("id, create_time, status, remark, count")
            ->where($map)->order("id DESC")->find();

        if ($arrSms) {
            Db::table('sms_code')->where("id", $arrSms["id"])->setInc("count");

            if ($arrSms["remark"] == $code) {
                if (time() < ($arrSms["create_time"]+config("sms_overtime"))) {
                    if ($arrSms["count"] > config("sms_overcount")) {
                        return array('state'=>FALSE,'code'=>10001,'message'=>'验证码已失效');
                    }
                } else {
                    return array('state'=>FALSE,'code'=>10001,'message'=>'验证码已超时');
                }
            } else {
                return array('state'=>FALSE,'code'=>10001,'message'=>'短信验证码错误');
            }
        } else {
            return array('state'=>FALSE,'code'=>10001,'message'=>'短信验证码错误');
        }

        return array('state'=>TRUE,'code'=>10000,'message'=>'');
    }
上一篇 下一篇

猜你喜欢

热点阅读