PHP手机发送验证码实例

<?php
//加上手机号亲测没有问题
$tel="18201129310";//电话号码
$m=randpw(6);//生成随机字符串
//调用发送短信的方法
$sms = send_sms($tel, "正在注册十里,验证码是".$m."【十里】");
//这里send_sms第二个参数是短信内容报备,格式是固定死的,验证码必须是变量不能写死的字符串,
 
//发送短信
function send_sms($phone,$content){
$target = "http://sms.chanzor.com:8001/sms.aspx";//短信接口地址
$account = 'shili';//短信帐号
$password = 'shili123';//短信密码
//替换成自己的测试账号,参数顺序和wenservice1对应
$post_data = "action=send&userid=&account={$account}&password={$password}&mobile={$phone}&sendTime=&content=".rawurlencode($content);
$gets = agm_http_post($target,$post_data);//调用CURL_POST
var_dump($gets);die;
$start=strpos($gets,"<?xml");
$data=substr($gets,$start);
$xml=simplexml_load_string($data);
 
$res = (json_decode(json_encode($xml)));
if($res->returnstatus=="Success"){
return 1;
}else{
return 2;
}
}
function agm_http_post($url,$param){
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
}//判断是否用https
if (is_string($param)) {
$strPOST = $param;
} else {
$aPOST = array();
foreach($param as $key=>$val){
$aPOST[] = $key."=".urlencode($val);
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($oCurl, CURLOPT_POST,true);
curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if(intval($aStatus["http_code"])==200){
return $sContent;
}else{
return false;
}
}
//生成随机数接口
function randpw($len=8,$format='NUMBER'){
$is_abc = $is_numer = 0;
$password = $tmp ='';
switch($format){
case 'ALL':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
break;
case 'CHAR':
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case 'NUMBER':
$chars='0123456789';
break;
default :
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
break;
}
mt_srand((double)microtime()*1000000*getmypid());
while(strlen($password)<$len){
$tmp =substr($chars,(mt_rand()%strlen($chars)),1);
if(($is_numer <> 1 && is_numeric($tmp) && $tmp > 0 )|| $format == 'CHAR'){
$is_numer = 1;
}
if(($is_abc <> 1 && preg_match('/[a-zA-Z]/',$tmp)) || $format == 'NUMBER'){
$is_abc = 1;
}
$password.= $tmp;
}
if($is_numer <> 1 || $is_abc <> 1 || empty($password) ){
$password = randpw($len,$format);
}
return $password;
 
}
?>

转载于:https://www.cnblogs.com/xiaoqiangjun/p/7391505.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值