How to send order details from PHP website to whatsapp

131 views Asked by At

I am building a website in which i want that when a user places an order, store owner should receive the order details in his whatsapp. Like First and last name, address, email, mobile and products which he purchased. Currently i tried Twilio for this purpose, but i don't want to use any 3rd party for this. I want to use only CORE PHP and whatsapp business api or library to achieve this.

Here is what i have done so far.

`<?php
require 'vendor/autoload.php';
use Twilio\Rest\Client;

$accountSid = '0000-0000-0000-0000-00000';
$authToken  = '0000-0000-00000-00000-00000';
$twilioNumber = 'whatsapp:+12000000000'; 

$whatsappNumber = 'whatsapp:+1255844885454'; 

$firstName = $_POST['first_name'];
$lastName = $_POST['last_name'];
$address = $_POST['address'];
$townCity = $_POST['town_city'];
$mobile = $_POST['mobile'];
$totalprice = $_POST['totalprice'];

$orderDetails = "First Name: $firstName\nLast Name: $lastName\nAddress: $address\nTown/City: $townCity\nMobile: $mobile\nTotal Amount: Rs$totalprice";

$client = new Client($accountSid, $authToken);

try {
    $message = $client->messages->create(
        $whatsappNumber,
        array(
            'from' => $twilioNumber,
            'body' => $orderDetails
        )
    );

    header("Location: checkout.php");
    exit();
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}
`
1

There are 1 answers

0
Harsh Raj Dubey On

Facebook does not provides it's WhatsApp business API to individuals or small businesses. You must buy the API from 3rd party like Wati, Twillo, etc. or you need to reach Facebook support directly requesting for an separate API. For example Wati and Twilio both provides PHP sample codes on their documentation and they can handle heavy loads easily.

Another way to achieve automatic Whatsapp message sending is to use Unofficial APIs which are way cheaper but has a very high risks of getting your number blocked straightaway which leads to poor client impression as the contact number needs to be changed once blocked.

The above code you shared just looks like a sample code of twilio api. It works correctly once the plans are active on your account and correct API key have been entered.

Facebook Offical Documentation Links provided https://developers.facebook.com/docs/whatsapp/cloud-api https://developers.facebook.com/docs/whatsapp/on-premises