How To Send Images on WhatsApp using PHP

May 31, 2019
Written by
Felistas Ngumi
Contributor
Opinions expressed by Twilio contributors are their own

Copy of Photo blog Header 2.png

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.

  1. PHP
  2. Twilio Account activated with WhatsApp Sandbox Channel.
  3. Twilio PHP SDK

Getting Started

In your preferred terminal, run the following commands:

$ mkdir demo
$ cd demo
$ touch twilioWhatsAppMessaging.php .env
$ composer require twilio/sdk vlucas/phpdotenv

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.

Twilio accoutn dashboard

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.

WhatsApp 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

<?php

require __DIR__ . "/vendor/autoload.php";

use Twilio\Rest\Client;

$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();

$sid    = getenv('TWILIO_SID');
$token  = getenv('TWILIO_TOKEN');


$twilio = new Client($sid, $token);

$message = $twilio->messages
                 ->create(
                     "whatsapp:+254712345678",
                     [
                              "mediaUrl" => ["https://images.unsplash.com/photo-1431250620804-78b175d2fada?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1600&h=900&fit=crop&ixid=eyJhcHBfaWQiOjF9"],
                              "from" => "whatsapp:+14155238886",
                              "body" => "Snacks maybe?"
                     ]
                 );

Remember to replace +254712345678 with the phone number that will receive the message.

In your .env file add:

TWILIO_SID=your_twilio_sid
TWILIO_TOKEN=your_twilio_token

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 and let’s connect and engage on Twitter!