Receive WhatsApp Media Messages Using PHP

Overview

This guide shows how to receive WhatsApp text messages on a phone number registered against your WhatsApp Business Account. Typical use cases for receiving messages include customer support,  opt-in and opt-out messages, and other customer interactions.

You can start receiving WhatsApp text messages using our APIs. Follow these instructions.

Prerequisites

To get started, you need a Plivo account — sign up with your work email address if you don’t have one already. If this is your first time using Plivo APIs, follow our instructions to set up a PHP development environment.

To receive incoming messages, you must have an onboarded WhatsApp account with inbound-capable numbers registered against it. If a number is in connected state, it can receive incoming messages.

Create an Server to receive messages

Create a file called receive_whatsapp.php and paste into it this code.

<?php

namespace App\Http\Controllers;
require '../../vendor/autoload.php';
use Plivo\RestClient;
use Illuminate\Http\Request;

class WhatsappController extends Controller
{
    public function receiveWhatsapp()
    {
        $from_number = $_REQUEST[""From""];
        $to_number = $_REQUEST[""To""];
        $media0 = $_REQUEST[""media0""] ?? '';
        $text = $_REQUEST[""Body""] ?? '';

        echo ""Message received - From: $from_number, To: $to_number, Media Attachment: $media0"";

        if ($text != '') {
            echo ""Text: $text"";
        }
    }
}

Configure a webhook URL in your WhatsApp Business Account

Add or update a webhook URL against a WhatsApp Business Account. Once you’ve done this, you should be able to receive incoming messages.

Test

Send a text message to the Plivo number you specified using any phone.