速码云短信接口嵌入网站:极简实现教程
速码云(SumCodeCloud)短信接口是常用的第三方短信服务,嵌入网站的核心是通过后端语言调用其 API 接口,实现「前端触发请求→后端校验并调用速码云 API→返回结果」的流程。以下以 PHP(最通用的网站后端语言) 为例,提供极简可运行的代码示例,同时补充前端交互逻辑。
一、前置准备
注册速码云账号:完成企业认证,获取 API ID/API KEY(在速码云控制台「短信接口」模块查看);
配置短信模板:在速码云后台创建验证码模板(如:您的验证码是{code},有效期5分钟),记录模板 ID;
服务器环境:确保网站服务器支持 PHP(5.6+),并开启curl扩展(绝大多数虚拟主机 / 服务器默认支持)。
二、核心实现步骤(前端 + 后端)
1. 前端页面(触发验证码发送)
创建一个简单的 HTML 页面,包含手机号输入框、发送按钮、验证码输入框,通过 AJAX 请求后端接口:
html
预览
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>速码云短信接口示例</title>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
</head>
<body>
<div>
<label>手机号:</label>
<input type="tel" id="mobile" placeholder="请输入11位手机号">
<button id="sendBtn">发送验证码</button>
</div>
<div style="margin-top: 10px;">
<label>验证码:</label>
<input type="text" id="code" placeholder="请输入验证码">
<button id="verifyBtn">验证</button>
</div>
<script>
// 发送验证码按钮点击事件
$('#sendBtn').click(function() {
const mobile = $('#mobile').val().trim();
// 简单手机号校验
if (!/^1[3-9]\d{9}$/.test(mobile)) {
alert('请输入正确的手机号');
return;
}
// 禁用按钮,防止重复点击
$(this).attr('disabled', true).text('发送中...');
// AJAX请求后端接口
$.ajax({
url: 'send_sms.php', // 后端接口文件路径
type: 'POST',
data: { mobile: mobile },
dataType: 'json',
success: function(res) {
if (res.code === 200) {
alert('验证码发送成功!');
// 60秒倒计时
let count = 60;
$('#sendBtn').text(count + '秒后重发');
const timer = setInterval(() => {
count--;
$('#sendBtn').text(count + '秒后重发');
if (count <= 0) {
clearInterval(timer);
$('#sendBtn').attr('disabled', false).text('发送验证码');
}
}, 1000);
} else {
alert('发送失败:' + res.msg);
$('#sendBtn').attr('disabled', false).text('发送验证码');
}
},
error: function() {
alert('网络错误,请重试');
$('#sendBtn').attr('disabled', false).text('发送验证码');
}
});
});
// 验证码验证按钮(可选,仅演示)
$('#verifyBtn').click(function() {
const mobile = $('#mobile').val().trim();
const code = $('#code').val().trim();
$.ajax({
url: 'verify_sms.php',
type: 'POST',
data: { mobile: mobile, code: code },
dataType: 'json',
success: function(res) {
if (res.code === 200) {
alert('验证成功!');
} else {
alert('验证失败:' + res.msg);
}
}
});
});
</script>
</body>
</html>
2. 后端发送验证码接口(send_sms.php)
核心逻辑:接收前端手机号 → 调用速码云短信 API → 存储验证码(用于后续验证)。
php
运行
<?php
// 1. 速码云接口配置(替换为你的实际信息)
$apiId = "你的速码云API ID"; // 控制台获取
$apiKey = "你的速码云API KEY"; // 控制台获取
$templateId = "你的短信模板ID"; // 验证码模板ID
$signName = "你的短信签名"; // 速码云审核通过的签名
// 2. 接收并校验前端参数
$mobile = $_POST['mobile'] ?? '';
if (!preg_match('/^1[3-9]\d{9}$/', $mobile)) {
exit(json_encode(['code' => 400, 'msg' => '手机号格式错误']));
}
// 3. 生成6位验证码
$code = rand(100000, 999999);
// 4. 调用速码云短信API(核心步骤)
$url = "https://api.sumcodecloud.com/sms/send"; // 速码云官方接口地址(以实际为准)
$postData = [
'apiId' => $apiId,
'apiKey' => $apiKey,
'mobile' => $mobile,
'templateId' => $templateId,
'params' => json_encode(['code' => $code]), // 模板参数,需和模板匹配
'signName' => $signName
];
// 使用curl发送POST请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 调试时关闭SSL验证,生产环境开启
$response = curl_exec($ch);
curl_close($ch);
// 5. 解析接口返回结果
$res = json_decode($response, true);
if (!$res) {
exit(json_encode(['code' => 500, 'msg' => '接口请求失败']));
}
// 6. 速码云返回成功则存储验证码(这里用session,生产环境建议用Redis/数据库)
session_start();
if ($res['code'] === 0) { // 速码云成功码以官方文档为准
$_SESSION['sms_code_' . $mobile] = [
'code' => $code,
'expire' => time() + 300 // 5分钟有效期
];
exit(json_encode(['code' => 200, 'msg' => '发送成功']));
} else {
exit(json_encode(['code' => 400, 'msg' => '发送失败:' . $res['msg']]));
}
?>
3. 后端验证码验证接口(verify_sms.php)
用于验证用户输入的验证码是否正确:
php
运行
<?php
session_start();
// 1. 接收参数
$mobile = $_POST['mobile'] ?? '';
$code = $_POST['code'] ?? '';
// 2. 校验参数
if (empty($mobile) || empty($code)) {
exit(json_encode(['code' => 400, 'msg' => '参数缺失']));
}
// 3. 获取存储的验证码
$stored = $_SESSION['sms_code_' . $mobile] ?? [];
if (empty($stored)) {
exit(json_encode(['code' => 400, 'msg' => '验证码不存在或已过期']));
}
// 4. 校验验证码和有效期
if ($stored['code'] != $code) {
exit(json_encode(['code' => 400, 'msg' => '验证码错误']));
}
if ($stored['expire'] < time()) {
unset($_SESSION['sms_code_' . $mobile]); // 过期删除
exit(json_encode(['code' => 400, 'msg' => '验证码已过期']));
}
// 5. 验证成功,清除验证码(防止重复使用)
unset($_SESSION['sms_code_' . $mobile]);
exit(json_encode(['code' => 200, 'msg' => '验证成功']));
?>
三、关键注意事项
接口地址确认:速码云的正式接口地址请以官方文档为准,上述https://api.sumcodecloud.com/sms/send为示例;
参数格式:params参数需为 JSON 字符串,且字段名要和短信模板中的变量(如{code})完全一致;
安全防护:
生产环境需增加频率限制(如单个手机号 1 分钟内最多发送 1 次),防止盗刷;
建议将apiId/apiKey存储在服务器配置文件中,不要直接写在业务代码里;
开启 curl 的 SSL 验证(删除CURLOPT_SSL_VERIFYPEER, false),避免数据泄露;
存储优化:示例中用session存储验证码,生产环境建议用 Redis(支持过期时间,性能更高)。
四、测试步骤
将上述 3 个文件上传到你的 PHP 服务器;
替换send_sms.php中的apiId/apiKey/templateId/signName为实际值;
访问 HTML 页面,输入手机号点击「发送验证码」,查看手机是否收到短信;
输入验证码点击「验证」,测试验证逻辑。
总结
速码云短信接口嵌入核心是「前端触发请求→后端调用 API→存储验证码→验证验证码」,核心依赖 curl 调用第三方 API;
关键配置项(API ID/KEY、模板 ID、签名)必须与速码云控制台一致,否则接口调用失败;
生产环境需补充频率限制、SSL 验证、安全存储等防护措施,避免接口被恶意利用。