本教程織夢短信接口插件代碼基于微米短信平臺(www.weimi.cc)的一個織夢短信接口。織夢58分享給大家。
具體代碼
1、存儲驗證碼到數據庫,需要新建建一個數據表。 后臺 ---- 系統 ---- sql命令行工具,運行以下代碼:(注意表前綴)
1
2
3
4
5
6
7
8
9
|
DROP TABLE IF EXISTS `dede_sms`;
CREATE TABLE `dede_sms` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`phone` varchar(15) NOT NULL DEFAULT '',
`code` varchar(8) NOT NULL DEFAULT '',
`created_at` int(10) NOT NULL DEFAULT '0',
`expire_at` int(10) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
|
2、新建 WMsendSms.PHP 文件,放在 /include 目錄下。具體代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
<?php
if(!defined('DEDEINC')) exit('Request Error!');
//發送短信
//$cid 短信模板CID
function sendSms($mob,$cid = '微米短信模板查看CID')
{
$res = validatePremise($mob);
if($res){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.weimi.cc/2/sms/send.html");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
/*
傳入模板參數。短信模板內容示例:
【微米】您的驗證碼是:%P%,%P%分鐘內有效。如非您本人操作,可忽略本消息。
傳入兩個參數:
p1:610912
p2:3
最終發送內容:
【微米】您的驗證碼是:610912,3分鐘內有效。如非您本人操作,可忽略本消息。
*/
$uid = '微米查看去';
$pas = '微米查看去';
$p1 = createRandomCoder(4);
$p2 = 5; //分鐘
curl_setopt($ch, CURLOPT_POSTFIELDS, "uid=$uid&pas=$pas&mob=$mob&cid=$cid&p1=$p1&p2=$p2&type=json");
$res = curl_exec( $ch );
curl_close( $ch );
$arr = json_decode($res,true);
if($arr['code'] == 0){
saveCode($mob,$p1);
return "短信發送成功!";
}
return "短信發送失敗!";
}else{
return "短信發送頻繁,請稍后再發送!";
}
}
//生成隨機字符串
//$len 要生成的隨機字符串長度
//$type 隨機碼類型:0,數字+大小寫字母;1,數字;2,小寫字母;3,大寫字母;4,特殊字符;-1,數字+大小寫字母+特殊字符
function createRandomCoder($len,$type = '0')
{
$arr = array(1 => "0123456789", 2 => "abcdefghijklmnopqrstuvwxyz", 3 => "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 4 => "~@#$%^&*(){}[]|");
if ($type == 0) {
array_pop($arr);
$string = implode("", $arr);
} elseif ($type == "-1") {
$string = implode("", $arr);
} else {
$string = $arr[$type];
}
$count = strlen($string) - 1;
$code = '';
for ($i = 0; $i < $len; $i++) {
$code .= $string[rand(0, $count)];
}
return $code;
}
//保存驗證碼
function saveCode($phone,$code,$time = 5)
{
global $dsql;
$created_at = time();
$expire_at = time() + ($time * 60);
$code = strtolower($code);
$sql = "INSERT INTO `dede_sms`(`phone`,`code`,`created_at`,`expire_at`) VALUES ('$phone','$code','$created_at','$expire_at')";
return $dsql->ExecuteNoneQuery($sql);
}
//檢查手機號,驗證碼
function validateCode($phone,$code)
{
global $dsql;
$code = strtolower($code);
$current = time();
$sql = "SELECT `id` FROM `dede_sms` WHERE `phone` LIKE '$phone' AND `code` LIKE '$code' AND `expire_at` > '$current' ";
$row = $dsql->GetOne($sql);
if(is_array($row)){
return true;
}else{
return false;
}
}
//檢查是否發送:防止惡意刷短信
//$phone 手機號 www.dede58.com下載
//$time 有效時間 (分鐘)
function validatePremise($phone,$time = '1')
{
global $dsql;
$row = $dsql->GetOne("SELECT `id`,`expire_at` FROM `dede_sms` WHERE `phone` LIKE '$phone' ");
if(is_array($row)){
if( time() < $row['expire_at'] ){
return false;
}else{
$dsql->ExecuteNoneQuery("DELETE FROM `dede_sms` WHERE id=".$row['id']);
}
}
return true;
}
|
提示:表的前綴改為你的表前綴,這樣就完成了織夢短信接口。