Home >>PHP API >SMS Gateway Integration in PHP
Now a days SMS Gateway features we can use for the various purpose in the web application. like OTP verification user account authentication, for any transaction, sending notification message to users mobile number etc.
Its easy to send SMS using PHP script. For this you just need to select a good SMS gateway vendors(provider) and purchase a suitable package according to your requirements.
In this tutorial, we are going to guide you how you can integrate SMS gateway API using PHP easily.
To integrate a SMS Gateway in website is Quiet simple and easy.
In the given below example you can easily send SMS to any your customers or users mobile number.
We are going to guide you through our example, send dynamic messages to any dynamic number after any form submission.
Normally SMS gateway Provider provides callback URL where we can Pass some Parameters. for example Reciever number, Sender Number, Capaign Name, message Content , API Key etc.
These Parameters may differ according to Message Provider. You need to pass parameters accordingly.
<?php //Your authentication key $authKey = "YourAuthKey"; //Multiple mobiles numbers separated by comma $mobileNumber = "9015501897,8588829328"; //Sender ID,While using route4 sender id should be 6 characters long. $senderId = "PHPTPN"; //Your message to send, Add URL encoding here. $message = urlencode("Welcome to the world of Test API"); //Define route $route = "default"; //Prepare you post parameters $postData = array( 'authkey' => $authKey, 'mobiles' => $mobileNumber, 'message' => $message, 'sender' => $senderId, 'route' => $route ); //API URL $url="http://sms.phptpoint.com/api/sendhttp.php"; // init the resource $ch = curl_init(); curl_setopt_array($ch, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postData //,CURLOPT_FOLLOWLOCATION => true )); //Ignore SSL certificate verification curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //get response $output = curl_exec($ch); //Print error if any if(curl_errno($ch)) { echo 'error:' . curl_error($ch); } curl_close($ch); echo $output; ?>
<?php extract($_POST); if(isset($register)) { $mob=$_REQUEST['mobile']; $msg=$_REQUEST['msg']; //Send sms to sender and reciever $senderId = "PHPTPN"; $route = 4; $campaign = "OTP"; $sms = array( 'message' => "Message : $msg", 'to' => array($mob) ); //Prepare you post parameters $postData = array( 'sender' => $senderId, 'campaign' => $campaign, 'route' => $route, 'sms' => array($sms) ); $postDataJson = json_encode($postData); $url="http://login.bulksms365.in/api/v2/sendsms"; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "$url", CURLOPT_RETURNTRANSFER => true, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => $postDataJson, CURLOPT_HTTPHEADER => array( "authkey: 7720ARd4rlCOX5ad7", "content-type: application/json" ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); //Send sms to sender and reciever $err="<h4 style='color:green'>Message Send successfully</h4>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Send Dynamic Message </title> <STYLE> .panel-body h2{ text-align:center; font-weight:bold; margin-top:20px; margin-bottom:10px; } </STYLE> <!-- Bootstrap Core CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body style=" background-color: rgb(0, 36, 74);"> <div class="container" > <div class="row" style="margin-top:20px"> <div class="col-md-6 col-md-offset-4"> <div class="login-panel panel panel-default"> <div class="panel-heading"> <h3 class="panel-title">Please Register</h3> </div> <div class="panel-body"> <form method="post" enctype="multipart/form-data"> <fieldset> <div class="form-group"> <label>Student Mobile Number</label> <input class="form-control" value="<?php echo $mobile; ?>" name="mobile" type="number" required placeholder="Enter Your mobile"> </div> <div class="form-group"> <label>Enter Your message</label> <textarea class="form-control" name="msg"></textarea> </div> <div class="form-group"> <input name="register" type="submit" value="Send Message" class="btn btn-primary"> </div> <div class="form-group"> <label> <?php echo @$err;?> </label> </div> </fieldset> </form> </div> </div> </div> </div> </div> </body> </html>