How to use PHP to implement SMS verification code to send

Source: Internet
Author: User
I in a number of third-party SMS service providers choose the Cloud chip network this short-message service provider, I will try to use the simplest way to help the vast number of developers to solve the SMS Verification Code function module implementation.

Again before I also refer to most of the online blog, and most are the cloud network of the demo to move up, for me this front-end personnel, there is no clue, so I will be careful to explain how to operate, and offer my source.

My business process is to send a verification code by clicking this button, triggering an AJAX request event, the mobile phone number to the background, the background generated verification code sent to the phone, and return this verification code to the foreground verification code verification.

The PHP backend code for the request is as follows

post.php

<?phpheader ("Content-type:text/html;charset=utf-8"); $apikey = "xxxxxxxxxxxxxxx"; Modify the login website for your apikey (https://www.yunpian.com) and get $mobile =$_post[' mobile '; Get incoming phone number//$mobile = "xxxxxxxxxxx";   Please use your phone number instead of $num = rand (1000,9999); Randomly generate a four-digit verification Code of Setcookie (' Shopcode ', $num); $text = "Mongolian goat" your captcha is ". $num." "; $ch = Curl_init (); /* Set authentication Mode */curl_setopt ($ch, Curlopt_httpheader, Array (' Accept:text/plain;charset=utf-8 ', ' content-type:application /x-www-form-urlencoded ', ' charset=utf-8 '));/* Set the return result to stream */curl_setopt ($ch, Curlopt_returntransfer, true); /* Set timeout time */curl_setopt ($ch, Curlopt_timeout, 10); /* Set Communication mode */curl_setopt ($ch, Curlopt_post, 1); curl_setopt ($ch, Curlopt_ssl_verifypeer, false); Get user Information $json_data = get_user ($ch, $apikey); $array = Json_decode ($json _data,true);//Echo ' <pre> ';p rint_r ($ Array); Send SMS $data=array (' text ' = = $text, ' apikey ' = $apikey, ' mobile ' + $mobile); $json _data = Send ($ch, $data); $ Array = Json_decode ($json _data,true);//Echo ' <pre> ';p rint_r ($array); Send Template SMSThe value needs to be encoded $DATA = Array (' tpl_id ' = ' 1 ', ' tpl_value ' = ' = ' #code # '). ' = '. UrlEncode ($num). ' & '. UrlEncode (' #company # '). ' = '. UrlEncode (' goat '), ' 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; To send a voice notification, be sure to report the template/* Template: The course #name# starts at #time#. Final Send result: Course depth learning starts at 14:00 */$tpl _id = ' xxxxxxx '; Modify the template for your own backstage Id$tpl_value = UrlEncode (' #time # '). ' = '. UrlEncode ($num). ' & '. UrlEncode (' #name # '). ' = '. UrlEncode (' sheep 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); /************************************************************************************///Get 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 ' operation completed without any errors ';}} ?>

This PHP background is i in the official demo on the modification, removed the voice verification function, only the SMS verification, and the return to the front end of the data only retained a four-digit verification code, convenient front-end verification code verification.

The official original demo connection is as follows ... Link

Index.html

The following code is to click and send an 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 input phone number  } ,  //DataType: "JSON",  success:function (data) {  console.log (data);  Layer.msg (' Verification code sent successfully, please pay attention to check! ');  Localstorage.setitem (' Code ', json.stringify (data)  },  error:function (err) {  console.log (err);}  });

Verification of Code validation

var code = json.parse (Localstorage.getitem (' Code ')) if ($ (' #code '). val ()! = code) {  layer.msg (' Verification Code input error ');  return false; }

Believe that you have seen these cases you have mastered the method, more wonderful please pay attention to the PHP Chinese network other related articles!

Related reading:

An example of Ajax asynchronous request technology

PHP High flow optimization?

Methods for generating Cartesian product from PHP custom functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.