Two-Way WhatsApp Messaging with PHP

A quick walk-through on how to seamlessly implement Sendchamp WhatsApp API with PHP

Aliyu Abubakar

Aug 19, 2021

Learn how to quickly send WhatsApp messages with PHP using sendchamp API. In this guide we will walk you through:

  1. Signing up free sendchamp account
  2. Activate a WhatsApp number
  3. Setting up a PHP Application
  4. Sending your WhatsApp message

Prerequisite

Before you can get started, you need the following already set up: You have PHP installed. If you don't, read about how to install it on your system here.

  • A sendchamp account
  • Signing up free sendchamp account

If you already have an account? skip to the next section You can sign up for a free sendchamp account here.

image

When you sign up, you'll have to verify your email address. This allows sendchamp to verify your identity.

After you've verified your email address, you'll be asked a question in order to get you started in a way that's relevant to you.

After you finish setting up your account, you'll have access to your dashboard where you can access your API Key and perform dashboard communication functions.

Active a WhatsApp Number

Visit your sendchamp dashboard and navigate to WhatsApp channel.

image

Click on activate number and choose a subscription plan that is suitable for you. You won’t be asked to pay for it until your number is active.

When you select a plan, you’ll be redirected to a page that would ask you to fill in the necessary information. Basic information, WhatsApp account information and your business information.

image

Setting up a PHP Application

First, you’ll need to create a file send.php to save your code.


<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.sendchamp.com/api/v1/whatsapp/message/send',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS =>'{
    "customer_mobile_number": "2348119974190",
    "message": "Hello from Olumide",
    "sender": "2348000000000"
    "type": "text"
}',
  CURLOPT_HTTPHEADER => array(
    'Content-Type: application/json',
    'Accept: application/json',
    'Authorization: Bearer SECRET_KEY'
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Replace CURLOTP_URL with either a test or live BASE URL depending on what you intend to do.

Replace Authorization under CURLOTP_HTTPHEADER with your test or live Access key depending on what you intend.

Parameters

  • Set message to the message want to send to your customer.
  • Set the recipient to the destination number.
  • Set the value of the sender to your activated WhatsApp number.
  • Set type as the type of message you want to send (text).

Sending your WhatsApp message

Now you can execute the code and send your WhatsApp message.

Run the following command:

php send.php 

The code you used in the send.php file sends a POST request to the sendchamp API endpoint to send your WhatsApp message.

© 2021 Sendchamp. All rights reserved.