How To Send a WhatsApp Message in 30 Seconds with PHP
Time to read: 2 minutes
WhatsApp is considered to be one of the top messaging apps globally, with over 1.5 billion users and over 60 billion messages sent per day. It has proven to be secure and reliable over the years and now with recent surveys, online businesses are using it as a communication medium to reach their customers other than the traditional way of sending SMS. In this tutorial, I’m going to take you through how to integrate WhatsApp Messaging into your application using the Twilio API for WhatsApp messaging. Here’s a short 30-second video on how you can achieve this after setting up.
This tutorial assumes you have basic knowledge of PHP and Unix/Linux commands. If not, don't be discouraged. I will explain every concept in detail.
Prerequisites
Ensure you have the following installed in your local development environment.
- PHP - Version 7.0 or higher.
- Composer
- Twilio Account activated with WhatsApp Sandbox Channel.
Getting Started
Open your terminal and run the following commands:
The above commands create a folder named demo-app
in which the twilioWhatsAppMessaging.php
and .env
files are created inside it.
Create a WhatsApp Twilio Project
After you have created and verified your account on Twilio, you will be redirected to the dashboard where you need to create your project. Under the Products tab, select Programmable SMS then click “Continue”. Next, you are required to key in your project name. I have named mine “Demo WhatsApp Messaging App”. To test your app, Twilio loads your account with a trial balance that you can use to purchase a number with Voice, SMS and MMS capabilities.
In the Programmable SMS dashboard, select “WhatsApp Beta” to activate your Sandbox.
You will be redirected to the dashboard above where you will be required to connect to your sandbox by sending a WhatsApp message through your device. In my case, I’m required to send join space-known
to +14155238886.
We are now all set to start sending WhatsApp messages! In your preferred IDE, open our project and add the following lines of code to start sending WhatsApp messages to twilioWhatsAppMessaging.php
Be sure to replace +254713456789
with your phone number that will receive the message
To install the official Twilio SDK and PHP Dotenv package and enable us to send WhatsApp messages and retrieve the environment variables respectively run:
Test Sending A WhatsApp Message
Head over to the Twilio dashboard to obtain the Twilio SID and Twilio Token and update the values in your .env
file.
To run our application, type php twilioWhatsAppMessaging.php
in your terminal. Voila! We’ve sent our first WhatsApp message.
Conclusion & Video
In this tutorial, we have learned how to send WhatsApp messages using the Twilio API for WhatsApp messaging. Scale your businesses and connect more with your customers where they are most; online! I have created a video version of this tutorial for you to watch.
You can find the full code on Github!
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.