限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。

PHP高级

实例 | 使用云片短信接口发送网站验证码

云片官网:https://www.yunpian.com/ 默认是有10条免费短信的 html var sends = { send:function(){ var myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0,6,7,8]{1})|(18[0-9]{1}))+\d{8})$/; var n

作者
王明昌
发布时间
最后更新
云片官网:https://www.yunpian.com/ 默认是有10条免费短信的 html

PHP方法
public function userverify() {
      $str = "0123456789";   //   输出字符集
      $n = 6;   //   输出串长度
      $len = strlen($str)-1;
      $verify='';
      for($i=0 ; $i<$n; $i++){
          $verify.=  $str[rand(0,$len)];
      }
      $item= $_POST['item'];
      $msg = Check::rule(array(
          array(check::mobile($item) && check::must($item), '请填写正确的手机号码!'),
      ));
      if ($msg !== true) {
          $this->jserror($msg);
      }
      $type = $_GET['type'];
      if($item){

          if(!empty($type)){
              if (!empty($_SESSION['message_verify'.$item.$type])) {
                  $this->jserror('已经发送至您手机,请稍候再试');
              }
          }else{
              if (!empty($_SESSION['message_verify'.$item])) {
                  $this->jserror('已经发送至您手机,请稍候再试');
              }
          }

  
          if(!empty($type)){
              $_SESSION['message_verify'.$item.$type] = $verify;
          }else {
              $_SESSION['message_verify'.$item] = $verify;
          }
          $_SESSION['verify']=$verify;
         
        
          $sms = new sendsms();
          $apikey = "b12a3c784ccc0000002bb02bc6481d7"; //请用自己的apikey代替
          $mobile = $item; //请用自己的手机号代替
          $text="【王明昌博客】您的验证码是".$verify."。如非本人操作,请忽略本短信";
          $successsmg = $sms->send_sms($apikey,$text,$mobile);
          /* 这里的$text要和$apikey 申请的保持一致,短信是发不出去的*/

          if($successsmg){
              $this->jssuccess('已经发送至您手机');
          }
           
      }else {
          $this->jserror('获取验证码失败!');
      }
  }
sendsms.class.php
sock_post($url, $post_string);
}

/**
* 模板接口发短信
* apikey 为云片分配的apikey
* tpl_id 为模板id
* tpl_value 为模板值
* mobile 为接受短信的手机号
*/
function tpl_send_sms($apikey, $tpl_id, $tpl_value, $mobile){
	$url="http://yunpian.com/v1/sms/tpl_send.json";
	$encoded_tpl_value = urlencode("$tpl_value");  //tpl_value需整体转义
	$post_string="apikey=$apikey&tpl_id=$tpl_id&tpl_value=$encoded_tpl_value&mobile=$mobile";
	return $this->sock_post($url, $post_string);
}

/**
* url 为服务的url地址
* query 为请求串
*/
function sock_post($url,$query){
	$data = "";
	$info=parse_url($url);
	$fp=fsockopen($info["host"],80,$errno,$errstr,30);
	if(!$fp){
		return $data;
	}
	$head="POST ".$info['path']." HTTP/1.0\r\n";
	$head.="Host: ".$info['host']."\r\n";
	$head.="Referer: http://".$info['host'].$info['path']."\r\n";
	$head.="Content-type: application/x-www-form-urlencoded\r\n";
	$head.="Content-Length: ".strlen(trim($query))."\r\n";
	$head.="\r\n";
	$head.=trim($query);
	$write=fputs($fp,$head);
	$header = "";
	while ($str = trim(fgets($fp,4096))) {
		$header.=$str;
	}
	while (!feof($fp)) {
		$data .= fgets($fp,4096);
	}
	return $data;
}
}
?>
0/500
友善分享您的看法。
反馈邮箱