php Using Cloud Network to Realize SMS Verification Code Function Example Code

  • 2021-08-16 23:28:33
  • OfStack

This paper will take php as an example to introduce the realization of SMS verification code function.

Among the numerous third-party SMS service providers, I chose Cloud Film Network as a SMS service provider. This paper will try its best to use the simplest way to help developers solve the SMS verification code function module.

Again before I also referred to most of the online blog, most of the cloud film network demo intact to move up, for my front-end staff, there is no clue at all, so I will explain how to operate in detail, and offer my source code.

My business process is to trigger an ajax request event by clicking the button of Send Verification Code, send the mobile phone number to the background, generate the verification code in the background and send it to the mobile phone, and return this verification code to the foreground for verification of the verification code.

The requested php back-end code is as follows

post.php


<?php
header("Content-Type:text/html;charset=utf-8");
$apikey = "xxxxxxxxxxxxxxx"; // Modify to your apikey(https://www.yunpian.com) Get it after logging in to official website 
$mobile =$_POST['mobile']; // Get the incoming mobile phone number 
// $mobile = "xxxxxxxxxxx"; // Please use your mobile phone number instead 
$num = rand(1000,9999);   // Random generation 4 Verification code of bit number 
setcookie('shopCode',$num);
$text=" [Mengyangyang] Your verification code is ".$num." . ";
$ch = curl_init();

/*  Set the authentication method  */
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
/*  Set the returned result to stream  */
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

/*  Set timeout time */
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

/*  Set the communication mode  */
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

//  Obtain user information 
$json_data = get_user($ch,$apikey);
$array = json_decode($json_data,true);
// echo '<pre>';print_r($array);

//  Send a text message 
$data=array('text'=>$text,'apikey'=>$apikey,'mobile'=>$mobile);
$json_data = send($ch,$data);
$array = json_decode($json_data,true);
// echo '<pre>';print_r($array);

//  Send Template SMS 
//  Need to be right value Encode 
$data = array('tpl_id' => '1', 'tpl_value' => ('#code#').
'='.urlencode($num).
'&'.urlencode('#company#').
'='.urlencode(' Mongolian sheep '), 'apikey' => $apikey, 'mobile' => $mobile);
// print_r ($data);
$json_data = tpl_send($ch,$data);
$array = json_decode($json_data,true);


echo $num;


//  Send voice verification code 
// $data=array('code'=>$num,'apikey'=>$apikey,'mobile'=>$mobile);
// $json_data =voice_send($ch,$data);
// $array = json_decode($json_data,true);
// echo $num;

//  When sending voice notification, be sure to report the template 
/* 
 Templates:   Curriculum #name# In #time# Here we go.   Final send result:   Deep learning of courses is in 14:00 Begin 
 */

$tpl_id = 'xxxxxxx'; // Modify the template for your own background filing id
$tpl_value = urlencode('#time#').'='.urlencode($num).'&'.urlencode('#name#').'='.urlencode(' Mongolian sheep ');
$data=array('tpl_id'=>$tpl_id,'tpl_value'=>$tpl_value,'apikey'=>$apikey,'mobile'=>$mobile);
$json_data = notify_send($ch,$data);
$array = json_decode($json_data,true);
// echo $num;


curl_close($ch);

/************************************************************************************/
// Acquisition of an account 
function get_user($ch,$apikey){
curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/user/get.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}
function send($ch,$data){
curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v2/sms/single_send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}
function tpl_send($ch,$data){
curl_setopt ($ch, CURLOPT_URL, 
'https://sms.yunpian.com/v2/sms/tpl_single_send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}
function voice_send($ch,$data){
curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v2/voice/send.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}
function notify_send($ch,$data){
curl_setopt ($ch, CURLOPT_URL, 'https://voice.yunpian.com/v2/voice/tpl_notify.json');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
$error = curl_error($ch);
checkErr($result,$error);
return $result;
}

function checkErr($result,$error) {
if($result === false)
{
echo 'Curl error: ' . $error;
}
else
{
//echo ' The operation completed without any errors ';
}
}

?> 

This php background was modified by me on the official demo, deleting the function of voice verification, only retaining SMS verification, and only retaining the 4-digit verification code for the data returned to the front end, which is convenient for the front end to verify the verification code.

The official original demo connection is as follows

index.html

The following code is to click and send ajax request, and save the requested verification code to localStorage


 $.ajax({ 
  type: "post", 
  url: "post.php", // Background code file name  
  data: {
  mobile:$('#phone').val()// Get the entered mobile phone number 
  }, 
  // dataType: "json", 
  success:function(data){ 
  console.log(data);
  layer.msg(' The verification code was sent successfully, please pay attention to check it! ');
  localStorage.setItem('code', JSON.stringify(data))
  }, 
  error:function(err){ 
  console.log(err); 
  } 
});  

Perform verification code verification


var code = JSON.parse(localStorage.getItem('code'))
if($('#code').val() != code ){
  layer.msg(' Wrong entry of verification code ');
  return false;
 }

Related articles: