How To Send Images on WhatsApp using PHP
Time to read: 1 minute
Introduction
WhatsApp is one of the most popular instant messaging applications used globally. It has over 450 million active users every day with an average user checking WhatsApp 23 times in a day. Modern businesses that have upheld online marketing tools in order to reach their customers have recorded tremendous growth in their sales and increased their customer base. In this tutorial, I’m going to take you through how to send WhatsApp messages with image attachments using PHP.
Prerequisites
Ensure you have the following installed in your local development environment.
- PHP
- Twilio Account activated with WhatsApp Sandbox Channel.
- Twilio PHP SDK
Getting Started
In your preferred terminal, run the following commands:
The above commands create a folder named demo
, in which the twilioWhatsAppMessaging.php
and .env
files are created inside of it. We have also installed the PHP Twilio package that will help us in sending WhatsApp messages alongside the PHP dotenv package that will enable us to access our environment variables.
After creating your account on Twilio, take note of your account’s SID and Token. We will need these two in order to send WhatsApp messages.
Head over to the All products & services
tab on the left and select the Programmable SMS
menu option. On the left menu under the Programmable SMS dashboard, select WhatsApp and activate your sandbox. You will be asked to send a message to the provided Twilio number. In my case, I’m asked to send join space-known
in order to activate the sandbox.
Now we are all set to start sending WhatsApp messages!
Next, open our project in your favorite editor and copy these few lines of code to twilioWhatsAppMessaging.php
Remember to replace +254712345678
with the phone number that will receive the message.
In your .env
file add:
Next, run php sendWhatsAppMessage.php
and voila! You should have received a WhatsApp message with an image attached to it.
Conclusion
In this tutorial, we have learned how to send WhatsApp messages with image attachments with PHP using the Twilio API for WhatsApp messaging. Happy hacking!
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.