網(wǎng)建短信通相比較阿里云要方便很多,不需要企業(yè)認證县恕,簽名和模板也不需要審核峰搪,注冊完成之后就可以使用了。
官網(wǎng)地址
短信類
<?php
/**
* 手機短信類
*/
namespace sendmsg;
use think\Db;
class Sms
{
/*
* 發(fā)送手機短信
* @param unknown $mobile 手機號
* @param unknown $content 短信內(nèi)容
*/
public function send($mobile, $content)
{
return $this->mysend_sms($mobile, $content);
}
/*
您于{$send_time}綁定手機號个榕,驗證碼是:{$verify_code}篡石。【{$site_name}】
-1 沒有該用戶賬戶
-2 接口密鑰不正確 [查看密鑰]不是賬戶登陸密碼
-21 MD5接口密鑰加密不正確
-3 短信數(shù)量不足
-11 該用戶被禁用
-14 短信內(nèi)容出現(xiàn)非法字符
-4 手機號格式不正確
-41 手機號碼為空
-42 短信內(nèi)容為空
-51 短信簽名格式不正確接口簽名格式為:【簽名內(nèi)容】
-6 IP限制
大于0 短信發(fā)送數(shù)量
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'] = '短信發(fā)送成功';
return $res;
}else{
$res['error'] = $result['message'];
return $res;
}
}
}
驗證短信是否可以發(fā)送
/**
* 發(fā)送驗證碼
* @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='')
{
//通過手機號判斷是否允許發(fā)送短信
$begin_add_time = strtotime(date('Y-m-d'));
$end_add_time = strtotime(date('Y-m-d')) + 24 * 3600;
//同一IP 每天只能發(fā)送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地址一天內(nèi)只能發(fā)送20條短信沟蔑,請勿多次獲取動態(tài)碼!');
}
//同一手機號,60秒才能提交發(fā)送一次
$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秒后才能再次發(fā)送短信狱杰,請勿多次獲取動態(tài)碼瘦材!');
}
//同一手機號,每天只能發(fā)送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'=>'同一手機一天內(nèi)只能發(fā)送5條短信,請勿多次獲取動態(tài)碼仿畸!');
}
$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'=>'手機短信發(fā)送失敗');
}
}else{
return array('state'=>FALSE,'code'=>10001,'message'=>'手機短信發(fā)送失敗');
}
}
獲取短信條數(shù)
/**
* 獲取數(shù)據(jù)條數(shù)
* @access public
* @author csdeshang
* @param type $condition 條件
* @return type
*/
public static function getSmsCount($condition) {
return Db::table('sms_code')->where($condition)->count();
}
短信記錄數(shù)據(jù)庫表
sms_code
短信驗證
配置
'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'=>'');
}