|
验证码短信适用于哪些场景?
验证码短信模版可用到的场景很多,例如注册时发送验证码短信、登录时发送验证码短信、找回/修改密码时发送验证码短信
短信内容前面中【】是什么?
短信内容【】指的是短信签名。 1、短信签名建议为用户真实应用名/网站名/公司名,提交后需审核
如何选择一个稳定的短信平台
首先就是验证码到达速度要在5秒内,能够简单调用,最主要短信平台经营的时间要很长,才会比较稳定,选择高新技术企业才是明选,能够自带异常号码失败重发,避免短信收不掉的情况,还要具备防攻击 ,能够控制每天的发送量,才是首先,我觉得这个才是最基本的吧。
代码调用示例
//接口类型:触发短信接口,支持发送验证码短信、订单通知短信等。
// 账户注册:请通过该地址开通账户:http://user.ihuyi.com/?jmzFP2
// 注意事项:
//(1)调试期间,请使用用系统默认的短信内容:您的验证码是:【变量】。请不要把验证码泄露给其他人。
//(2)请使用 APIID 及 APIKEY来调用接口,可在会员中心获取;
//(3)该代码仅供接入短信接口参考使用,客户可根据实际需要自行编写;
//开启SESSION
session_start();
header("Content-type:text/html; charset=UTF-8");
//请求数据到短信接口,检查环境是否 开启 curl init。
function Post($curlPost,$url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
$return_str = curl_exec($curl);
curl_close($curl);
return $return_str;
}
//将 xml数据转换为数组格式。
function xml_to_array($xml){
$reg = &#34;/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/&#34;;
if(preg_match_all($reg, $xml, $matches)){
$count = count($matches[0]);
for($i = 0; $i < $count; $i++){
$subxml= $matches[2][$i];
$key = $matches[1][$i];
if(preg_match( $reg, $subxml )){
$arr[$key] = xml_to_array( $subxml );
}else{
$arr[$key] = $subxml;
}
}
}
return $arr;
}
//random() 函数返回随机整数。
function random($length = 6 , $numeric = 0) {
PHP_VERSION < &#39;4.2.0&#39; && mt_srand((double)microtime() * 1000000);
if($numeric) {
$hash = sprintf(&#39;%0&#39;.$length.&#39;d&#39;, mt_rand(0, pow(10, $length) - 1));
} else {
$hash = &#39;&#39;;
$chars = &#39;ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz&#39;;
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
}
return $hash;
}
//短信接口地址
$target = &#34;http://106.ihuyi.com/webservice/sms.php?method=Submit&#34;;
//获取手机号
$mobile = $_POST[&#39;mobile&#39;];
//获取验证码
$send_code = $_POST[&#39;send_code&#39;];
//生成的随机数
$mobile_code = random(4,1);
if(empty($mobile)){
exit(&#39;手机号码不能为空&#39;);
}
//防用户恶意请求
if(empty($_SESSION[&#39;send_code&#39;]) or $send_code!=$_SESSION[&#39;send_code&#39;]){
exit(&#39;请求超时,请刷新页面后重试&#39;);
}
$post_data = &#34;account=用户名&password=密码&mobile=&#34;.$mobile.&#34;&content=&#34;.rawurlencode(&#34;您的验证码是:&#34;.$mobile_code.&#34;。请不要把验证码泄露给其他人。&#34;);
//查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID
//查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY
$gets = xml_to_array(Post($post_data, $target));
if($gets[&#39;SubmitResult&#39;][&#39;code&#39;]==2){
$_SESSION[&#39;mobile&#39;] = $mobile;
$_SESSION[&#39;mobile_code&#39;] = $mobile_code;
}
echo $gets[&#39;SubmitResult&#39;][&#39;msg&#39;]; |
|