How PHP uses cloud chip network to realize SMS verification code function

Source: Internet
Author: User
Tags urlencode
This article mainly introduces PHP using cloud chip network to implement SMS verification code function example code, small series feel very good, and now share to everyone, but also for everyone to do a reference. Let's take a look at it with a little knitting.

This article will take PHP as an example to introduce the implementation of the Web page SMS verification code function.

In a number of third-party SMS service providers I chose the Cloud chip network This text messaging service provider, this article 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 time-out */curl_setopt ($ch, Curlopt_timeout, 10);/* Set 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> ';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 SMS//need toValue is 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 complete 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; }

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.