You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

Send SMS Using PHP


By: Radha

Send SMS using PHP 

In this article, I have explained how to send an SMS from PHP to mobile phones.

Twilio API: 

Twilio is a cloud communications (IaaS) company based in San Francisco, California. It allows developers to programmatically send an SMS using its APIs. Twilio’s services are accessed over HTTP and are billed based on usage.

Step 1: 

First, we need to do is setup a free trial account in Twilio

URL: https://www.twilio.com/try-twilio

Image

Step 2: 

Next, we need to create a phone number for your account from which you can send the SMS

  1. Click -> “Phone Numbers” in the navigation bar
  1. Click -> “Get a number now” link to generate your own phone number

Image

Step 3: 

  1. Now “Click -> Get your first Twilio phone number”.

Image

Step 4: 

Once you click “Get your first Twilio phone number”, a new pop-up window will show your Twilio Phone Number. Now, click “Choose this Number”.

Image

Step 5: 

Next, you need to get your account SID and your authorization token.

  1. Click “Account” tab from the menu bar.

Image

b. Once you clicked Account tab, it redirects to your account page. You can get AccountSid and AuthToken from the account page.

Image

Step 6: 

HTML CODE:  // FileName: Index.php

<html> 

<head> 

Send SMS using PHP 

</head> 

<body> 

<form method="POST" action="SMS.php"> 

Mobile Number<input type="Text" name="Number"><br/> 

Type your Message <input type="Text" name="Message"><br/> 

<input type="Submit" value="Send SMS"> 

</form> 

</body> 

</html> 

PHP CODE:  //FileName: SMS.php

<?php 

require "twilio-php/Services/Twilio.php";  

// set your AccountSid and AuthToken from www.twilio.com/user/account

Image

Image

$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXX"; 

$AuthToken = "2bXXXXXXXXXXXXXXXXXXXXXXXXX"; 

$client = new Services_Twilio($AccountSid, $AuthToken); 

try { 

    $number=$_POST['Number']; 

    $msg=$_POST['Message']; 

    $message = $client->account->messages->create(array( 

        "From" => "+17015440243", // Paste your phone number here(Refer Step 4) 

        "To" => $number, 

        "Body" => $msg, 

    )); 

} catch (Services_Twilio_RestException $e) { 

    echo $e->getMessage(); 

} 

?> 

Note: Twilio provides a PHP library that makes it simple to work with SMS we need to include that library files in the server. Use below link to download library files.

Download Twilio SMS Library files from here: https://twilio-php.readthedocs.org/en/latest/

Step 7: Sending SMS from PHP.

Image

Step 8: Receiving an SMS.

Image

Step 9: How to verify Phone Number.

  1. Click “Products – > Phone numbers” in your account and it redirects to the “Phone Numbers” pages.

Image

b. Click “VERIFY CALLERS IDS”

c. Click “Verify a Number” button to verify the phone numbers you are going to send the SMS.

Image

Note: Trial accounts cannot send messages to unverified numbers; verify +9199XXX23867 at twilio.com/user/account/phone-numbers/verified, or purchase a Twilio number to send messages to unverified numbers.

Reference: https://www.youtube.com/watch?v=G4oluQf_7S4