When you are running business applications like ecommerce stores, web applications, customer-specific applications, and banking apps, you need to reach out to your customers by any medium of communication like email, calls, and SMS messages.
We know that billions of people use cell phones, and it makes it much easier to reach out to them. Here’s a research developed by Statista on the evaluation of cell phone usage since 2013 and it is forecasted that there will be around 5.07 billion cellphone users by 2019.
We have SMS services like Twilio to reach out our customers and take advantage of the figures. Twilio is a cloud communication platform which provides APIs of messaging, voice calls, and video calls.
In this article, we are using Twilio Messaging API on Cloudways server to send SMS messages to customers. I am assuming that you’ve already setup the new server on Cloudways and launched a PHP stack application.
Now, we have to follow 5 easy steps to setup Twilio API on Cloudways.
Let’s get started.
Step 1: Register Account on Twilio
You need to register an account on Twilio first. After signing up you will get your API credentials that includes Account SID and Auth Token. After registration, go to Account settings where you can see the secret Twilio API information.
Twilio provides two sets of API credentials if you have a trial account.
Login and head over Account settings where you’ll see these.
You can use TEST credentials for testing purpose in your apps but when you are upgrade to a premium account you will need the LIVE credentials. Since we have trial account, we will use the test credentials.
Step 2: Enable Geographic Permissions
Twilio provides its services worldwide but you can enable and disable it by configuring geographic permissions. Go to Account settings, scroll down, and you will see the settings for SMS and Voice geographic permissions.
We are setting up SMS API so click on SMS geographic permissions then filter your country to enable checkbox in it.
Step 3: Verify Numbers to Send Message
Twilio will ask you to verify your phone number on trial account. To verify the number move to Verification Page and enter your digits.
Click on the “Text Me” button and you will get a verification code. Enter the code in the next verification window and click on Submit button. You have successfully verified your number.
Step 4: Download Twilio Libraries
To send messages from Twilio on Cloudways, you need to create a cloud server.
After creating a server, launch PHP stack application and download Twilio API files in your projects. Let’s download the library from the SSH terminal inside Cloudways. You can find it in the Server Tab. Move it to your public_html folder by using this command:
$ cd application/{your app folder name}/public_html/
Now, run the following command to install Twilio library:
$ composer require twilio/sdk:4.11.0
Step 5: Create index.php File in Cloudways Server
It’s time to move to Cloudways server and create an index.php file to add some code. Move to server area and find your Master Credentials and connect to you server with FileZilla.
After connecting it to FileZilla, move to application/{your app folder}/public.html and edit index.php file by adding the following code:
<?php require 'vendor/twilio/sdk/Services/Twilio.php'; use Twilio\Rest\Client; // Your Account SID and Auth Token from twilio.com/console $sid = 'Axxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; $token = 'your Auth token'; $client = new Client($sid, $token); // Use the client to do fun stuff like send text messages! $client->messages->create( // the number you'd like to send the message to '+923463655371', array( // A Twilio phone number you purchased at twilio.com/console 'from' => '+12406086952', // the body of the text message you'd like to send 'body' => 'Hey shahroz! Good luck for the exam!' ) ); ?>
You might be interested at: How To Get Server And App SMS Alerts Using Twilio And CloudwaysBot
We have finally setup Twilio API on Cloudways server to send messages in a simple way. You can also customize it and use forms with validation strategies. Extend the functionality of this service as per your requirements.
Shahzeb Ahmed
Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]