Php uses the cloud film network to implement the text message verification code function sample code,
This article uses php as an example to introduce the implementation of the web text message verification code function.
Among the many third-party text message service providers, I chose the cloud film Network text message service provider. This article will try to use the simplest way to help developers solve the implementation of the SMS verification code function module.
I once again referred to most blogs on the Internet, and most of them moved the cloud film Network demo intact. For me, this front-end staff has no clue at all, therefore, I will explain in detail how to operate and present my source code.
My business flow is to trigger an ajax request event by clicking the send verification code button, send the mobile phone number to the background, and generate a verification code in the background to the mobile phone end, return the verification code to the front-end for verification.
The php backend code of the request is as follows:
Post. php
<? Phpheader ("Content-Type: text/html; charset = UTF-8"); $ apikey = "xxxxxxxxxxxxxxx"; // modify it to your apikey (https://www.yunpian.com) log on to the official website and get $ mobile =$ _ POST ['mobile']; // obtain the incoming mobile phone number // $ mobile = "xxxxxxxxxxx "; // Replace $ num = rand (,) with your mobile phone number; // generate a four-digit Verification Code setcookie ('shopcode', $ num ); $ text = "[mengyang] Your verification code is ". $ num. ". "; $ Ch = curl_init ();/* set the Verification 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 a stream */curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true);/* set the 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); // get user information $ json_data = Get_user ($ ch, $ apikey); $ array = json_decode ($ json_data, true); // echo '<pre>'; print_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>'; print_r ($ array ); // send template text message // encode the value $ data = array ('tpl _ id' => '1 ', 'tpl _ value' => ('# code #'). '= '. urlencode ($ num ). '&'. urlencode ('# company #'). '= '. urlen Code ('mengyang'), 'apikey' => $ apikey, 'mobile' => $ mobile); // print_r ($ data ); $ json_data = tpl_send ($ ch, $ data); $ array = json_decode ($ json_data, true); echo $ num; // send the 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; // send a voice notification, make sure to report the template/* template: course # name # Starts at # time. Final sending result: the deep learning course starts at */$ tpl_id = 'xxxxxxx '; // modify the template id for your own background report $ tpl_value = urlencode ('# time #'). '= '. urlencode ($ num ). '&'. urlencode ('# name #'). '= '. urlencode ('mengyangyang'); $ data = array ('tpl _ id' => $ tpl_id, 'tpl _ value' => $ tpl_value, 'apikey' => $ apikey, 'mobile' => $ mobile); $ json_data = policy_send ($ ch, $ data); $ array = json_decode ($ json_data, true ); // echo $ num; curl_close ($ ch ); /*************************************** ***** **************************************** /// Obtain the 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. yunp Vertex); 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: // signature); curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ data); $ result = curl_exec ($ ch); $ error = cu Rl_error ($ ch); checkErr ($ result, $ error); return $ result;} function voice_send ($ ch, $ data) {curl_setopt ($ ch, CURLOPT_URL, 'HTTP: // curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ data); $ result = curl_exec ($ ch); $ error = curl_error ($ ch ); checkErr ($ result, $ error); return $ result;} function yy_send ($ ch, $ data) {curl_setopt ($ ch, CURLOPT_URL, 'https: // voice. yunpi Vertex); 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 'no error after the operation is complete'; }}?>
I modified the php background on the official demo. I deleted the voice verification function and only kept text message verification, only the four-digit verification code is retained for the data returned to the front-end, so that the front-end can verify the verification code.
The official original demo connection is as follows:
Index.html
The following code clicks and sends an ajax request to save the request verification code to localStorage.
$. Ajax ({type: "post", url: "post. php ", // The Name Of The background code file data: {mobile: $ ('# phone '). val () // get the entered mobile phone number}, // dataType: "json", success: function (data) {console. log (data); layer. msg ('verification code has been sent successfully. Please check it! '); LocalStorage. setItem ('code', JSON. stringify (data)}, error: function (err) {console. log (err );}});
Verification code verification
Var code = JSON. parse (localStorage. getItem ('code') if ($ ('# Code'). val ()! = Code) {layer. msg ('verification code input error'); return false ;}
The above verification code function has been explained. If you need the source code, please click (Source Code) to download it on your own. I hope it will be helpful for your learning and support the help house.