Month: August 2021

send OTP using angular js and php
Angular JS

How To Send OTP Using Angular JS And PHP?

hello there, today we are going to discuss how can we send OTP using angular js and PHP? so in this tutorial, we need to understand some advanced topics of PHP, and also How API call in PHP? in this tutorial we will use API for sending the message, which is free to use. so let’s get started. which service we will use for send OTP using angular js and php? so in this tutorial, we gonna use the fast2sms service which provides 50 free credits for send SMS. this service actually provides us bulk SMS service but will use this service as OTP verification. if you also want to use this service then please visit this link. so we know that we are going to use the free2sms service but how do we donna calls the API this is the main part of this tutorial, so we get a link from the fast2sms service but we need to call that service so we are gonna use cURL for this operation. What is cURL? curl is used in command lines or scripts to transfer data. curl is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, set-top boxes, media players and is the Internet transfer engine for thousands of software applications in over ten billion installations. what cURL supports? DICT, FILE, FTP, FTPS, GOPHER, GOPHERS, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, HTTP/3, cookies, user+password authentication (Basic, Plain, Digest, CRAM-MD5, SCRAM-SHA, NTLM, Negotiate and Kerberos), file transfer resume, proxy tunneling and more. Let’s Move to the front-end side of this tutorial. so in the front-end, we will use HTML, CSS, Bootstrap, and jquery. so in this tutorial, we will use some jquery functions too, so it makes our page much creative. so let’s create a user form and go through it.
<!DOCTYPE html>
<html lang="en">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <title>OTP Verification Using Angular JS And PHP</title>
  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  <script src=""></script>
  <style type="text/css">
input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
<div class="container-fluid" ng-app="OTPApp" ng-controller="OTPC">
	<div class="row">
		<div class="col-md-12">
			<h1 class="text-center">OTP Verification Using Angular JS AND PHP</h1>
			<div class="d-flex justify-content-center">

				<div class="card" style="border-radius: 20px;overflow: hidden;">
					<div id="msg"> </div>
					<div class="card-header">
						<h4 class="text-info">Please Verify Your Mobile Number</h4>
					<div class="card-body">
					<div id="sendotp">
							<label>Enter Your Mobile Number</label>
							<input oninput="javascript: if (this.value.length > this.maxLength) this.value = this.value.slice(0, this.maxLength);"type = "number"maxlength = "10"
 							class="form-control" placeholder="please enter your mobile number here" id="mobile" ng-model="mobile" required/>
						<div id="verify">
								<label>Enter OTP</label>
								<input type="number" oninput="javascript: if(this.value.length > this.maxLength) this.value=this.value.slice(0,this.maxLength);" maxlength="6" class="form-control" placeholder="please enter otp here" ng-model="otp">
					<div class="d-flex justify-content-end card-footer">
						<button class="btn btn-primary" id="sendOtp" ng-click="sendOTP()">Send OTP</button>
						<button class="btn btn-success" id="verifyOTP" ng-click="verifyOTP()">Verify OTP</button>
  <!-- Bootstrap core JavaScript -->
  <script src="" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
  <script src="" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
  <script src="" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
this is the front-end but we need to apply some jquery here let’s generate it.
  	$(document).ready(function() {
  		$("#mobile").keyup(function() {
  			var number = $(this).val();
  			if(number.length == 10){
all the javascript or jquery code provide here you have to use inside the script tag. so you can call it properly. after complete these steps, your output should look like this. we have completed our front-end here now let’s go through the back-end of this tutorial. Let’s Move to the Back-end of the Send OTP Using Angular Js And PHP. so we completed the front-end so now we have to create a backend of the tutorial. so let’s create ajax for the calling API.
var app = angular.module('OTPApp',[]);
  		$scope.sendOTP = function(){
  		$scope.verifyOTP = function(){
so we completed the front-side and also we completed the API call for sending SMS using angular js and PHP. now only one thing remaining that is the main part of this tutorial sending SMS API and how to use we will use the API which is provided by the fast2sms, you can find it under the dashboard section inside the dev-API page where you can find your API let’s create an API for send SMS.

$data =json_decode(file_get_contents("php://input"),true);
$mobile = mysql_real_escape_string($data['mobile_number']);
$uotp = mysql_real_escape_string($data['otp']);
$ch = mysql_real_escape_string($data['submitFor']);
switch ($ch) {
	case 'submit_otp':
		$num = $mobile;			
		$otp = rand(100000,999999);
		$key ="your key";
		$route = "get from your dashboard";
		$senderid = "get from your dashboard";
		$message = "Hello your varification OTP is $otp";
		$language = "english";
		$url = "dev/bulkV2?".
		$curl = curl_init();
		curl_setopt_array($curl, array(
		CURLOPT_URL => $url,
		    "content-type: application/x-www-form-urlencoded"
	$response = curl_exec($curl);
	$err = curl_error($curl);
	if ($err) {
	  echo "cURL Error #:" . $err;
	} else {
		// echo $response;
	  echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
    <strong>Success !</strong> your OTP has been sent.
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
	case 'verify_otp':
		$user_otp = $uotp;
		$verify_otp = $_COOKIE['otp'];
		if($verify_otp == $user_otp){
			$response =array('res'=> '<div class="alert alert-success alert-dismissible fade show" role="alert">
    <strong>Success !</strong> your OTP successfully verified.
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
    setcookie("otp", "", time() - 3600);
echo json_encode($response);
			$response =array('info'=>'<div class="alert alert-danger alert-dismissible fade show" role="alert">
    <strong>Failed !</strong> Please check your OTP.
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
    <span aria-hidden="true">&times;</span>
    echo json_encode($response);

so now everything is done now you have to just run this file and check that you receive an SMS or not, if not then let us know in our comment section, and also if you want to learn that how to send E-mail using angular JS and PHP then please visit our this blog.