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
Step 2:
Next, we need to create a phone number for your account from which you can send the SMS
- Click -> “Phone Numbers” in the navigation bar
- Click -> “Get a number now” link to generate your own phone number
Step 3:
- Now “Click -> Get your first Twilio phone number”.
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”.
Step 5:
Next, you need to get your account SID and your authorization token.
- Click “Account” tab from the menu bar.
b. Once you clicked Account tab, it redirects to your account page. You can get AccountSid and AuthToken from the account page.
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
$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.
Step 7: Sending SMS from PHP.
Step 8: Receiving an SMS.
Step 9: How to verify Phone Number.
- Click “Products – > Phone numbers” in your account and it redirects to the “Phone Numbers” pages.
b. Click “VERIFY CALLERS IDS”
c. Click “Verify a Number” button to verify the phone numbers you are going to send the SMS.
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