How to Send SMS in PHP using Fast2SMS API


How to Send SMS in PHP using Fast2SMS API

In this tutorial, I will explain how to send SMS in PHP using fast2sms API in a step-by-step process. SMS sending is a very important part of any kind of web application that provides instant information to users’ phone numbers to verify OTP and sending credentials etc. It is one of the most required features used by developers and can be integrated with the help of APIs provided by the SMS providers.

Fast2SMS is an SMS service provider that provides various SMS services such as bulk SMS, Quick SMS, DLT SMS, etc. Bulk SMS is used by companies, schools, and colleges, etc. In this tutorial, we will use the quick SMS service by using the quick SMS API in our application. In quick SMS, users will get SMS from random numeric numbers. After creating an account in Fast2SMS, Rs. 50 credited to the user account for free.

Also read, Integrate Instamojo payment gateway in PHP step by step

Prerequisite:- Any PHP version, XAMPP, or WAMP server.

Required steps to send SMS in PHP using FastSMS API

Step 1:- Create an account on the Fast2SMS website.

Step 2:- After creating the account, in the dashboard, navigate to the dev API at the left side of the dashboard and copy the API key for later use as shown below.


How to Send SMS in PHP using Fast2SMS API

Step 3:- Just below the API key, go the API documentation and select the POST method from quick SMS API in PHP language as shown below.


How to Send SMS in PHP using Fast2SMS API

Step 4:- Now create a folder in the root directory of your local server. In my case, the folder name is smsinphp.

Step 5:- Now, create an index.php file inside the folder you have just created as shown below

index.php:-

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<div class="container" style="width: 50%;">  
  <!-- Content here -->
  <h5 class="text-center">Send SMS in PHP using fast2sms API</h5><hr><br>
  <form method="post" action="send_sms.php">
    <div class="mb-3">
      <label for="exampleFormControlInput1" class="form-label fw-bold">Phone No*</label>
      <input type="text" name="phone_no" class="form-control" id="exampleFormControlInput1">
    </div>
    <div class="mb-3">
      <label for="exampleFormControlTextarea1" class="form-label fw-bold">Message</label>
      <textarea class="form-control" name="message" id="exampleFormControlTextarea1" rows="3"></textarea>
    </div>
    <div class="mb-3">
      <button type="submit" name="submit" class="btn btn-primary">Submit</button>
    </div>
  </form>
    
</div>

<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js" integrity="sha384-IQsoLXl5PILFhosVNubq5LC7Qb9DXgDA9i+tQ8Zj3iwWAwPtgFTxbJ8NT4GN1R8p" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.min.js" integrity="sha384-cVKIPhGWiC2Al4u+LWgxfKTRIcfu0JTxR+EQDz/bgldoEyl4H0zUF0QKbrJ0EcQF" crossorigin="anonymous"></script>

Step 6:- Now create a PHP action file send_sms.php separately that was mentioned in the index.php file as shown below.

send_sms.php:-

<?php

if(isset($_POST['submit'])){
//get the inputs
	$phone = $_POST['phone_no'];
	$message = $_POST['message'];
	$fields = array(
		    "message" => $message,
		    "language" => "english",
		    "route" => "q",
		    "numbers" => $phone,
		);

		$curl = curl_init();

		curl_setopt_array($curl, array(
		  CURLOPT_URL => "https://www.fast2sms.com/dev/bulkV2",
		  CURLOPT_RETURNTRANSFER => true,
		  CURLOPT_ENCODING => "",
		  CURLOPT_MAXREDIRS => 10,
		  CURLOPT_TIMEOUT => 30,
		  CURLOPT_SSL_VERIFYHOST => 0,
		  CURLOPT_SSL_VERIFYPEER => 0,
		  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		  CURLOPT_CUSTOMREQUEST => "POST",
		  CURLOPT_POSTFIELDS => json_encode($fields),
		  CURLOPT_HTTPHEADER => array(
		    "authorization: your api key from fast2sms",
		    "accept: */*",
		    "cache-control: no-cache",
		    "content-type: application/json"
		  ),
		));

		$response = curl_exec($curl);
		$err = curl_error($curl);

		curl_close($curl);

		if ($err) {
		  echo "cURL Error #:" . $err;
		} else {
		  /*echo '<pre>';
		  echo $response;*/
		  echo '<b>SMS sent successfully on the number: '.$phone.'</b>';
		  header("refresh:5;url=index.php");

		}
	}

?>

Illustration of the above code:-

  • First of all, we will receive the form inputs with the help of PHP global variable $_POST and store.
  • Next, we will put the input fields in an array. The array index “route”=>”q” is used for sending quick SMS through quick SMS API that is available 24*7.
  • Now, we will put our API key as mentioned earlier in the CURLOPT_HTTPHEADER array as “authorization”.
  • Now, if you echo the response in the else condition of the last part of the above code then you can see the response as shown below

How to Send SMS in PHP using Fast2SMS API

Use the above code snippet just for checking the response of the message.

Step 6:- Now open the browser and enter the URL as shown below.

http://localhost/myprojects/smsinphp/index.php

Now, you can send the SMS as shown below


How to Send SMS in PHP using Fast2SMS API

Conclusion:- I hope this tutorial will help you to understand the concept of SMS sending. If there is any doubt then please leave a comment below.

Leave a Comment

thor311

thor311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

HOKI311

HOKI311

sbobet WAP

hoki311

sabung ayam online

hoki311

hoki311

live casino online

HOKI311

hoki311

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

mix parlay sbobet

hoki311

hoki311

hoki 311

togel hoki311

hoki311

THOR311

THOR311

THOR311

ceri188

ceri188

ceri188

ratu311 slot

THOR311

kupu178 toto hongkong

hoki311

hoki311

SABA Sport

sbobet

hoki311

THOR311

RATU311

kupu178 togel online

kupu178 slot demo

slot ratu311

pola slot gacor

ratu311

live casino online

hoki311

slot gacor online

hoki311

mix parlay

hoki311

sabung ayam online

hoki311

THOR311

THOR311

HOKI311

SBOBET WAP

Judi Bola Online

hoki311

hoki311

sabung ayam online

hoki311

hoki311

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

KUPU178

ceri188

ceri188

ceri188

togel online

hoki311

hoki311

sabung ayam online

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

KUPU178

KUPU178

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311