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;
}
?>
一个平凡的少年,不愿随大流,选择了编程之路。