Integrations

WhatsApp API in PHP: send a WhatsApp message from PHP and Laravel, a complete example

Sending a WhatsApp message with PHP through the official API: one cURL function for every call, sending an approved template and handling each answer, free text and message status, a Laravel example, and the common mistakes.

8 min read Updated 15 November 2026 ArabyBot team
The ArabyBot teamOfficial Meta partner · written from our own platform data
On this page 7
  1. What you need before the first line
  2. One function for every call
  3. Sending a template, and handling each answer
  4. Free text and the message status
  5. In Laravel
  6. Common PHP mistakes
  7. Frequently asked questions

The short answer

Sending a WhatsApp message from PHP needs no library: one cURL call to POST https://app.arabybot.com/api/v1/messages/template with your key in the Authorization header and a JSON body holding the recipient's number with its country code, the approved template's name and its variables. The answer returns the message id and status, and the reference field stops it repeating if your system retries. In Laravel the Http client does it in one statement.

Also asked as: «whatsapp api php» · «ارسال رسالة واتساب php» · «كود php لارسال رسالة واتساب» · «ارسال واتساب من لارافيل» · «whatsapp api php send message» · «laravel whatsapp api» · «php send whatsapp message»

What you need before the first line

  1. A WhatsApp number connected to ArabyBot

    On the official WhatsApp platform, with at least one approved template. Messages leave from your own number and show in the customer's conversation in the live chat.

  2. A plan that includes «Developer tools»

    WhatsApp Pro, WhatsApp Premium, Complete Plan or Ultimate Plan.

  3. A key

    Make it on the «Developer tools» page. It starts with abk_ and is shown once, so copy it into the ARABYBOT_API_KEY environment variable on your server.

One function for every call

The API is five addresses that all take and return JSON, so one cURL function covers them. A twenty-second timeout stops a request hanging in your page if the answer is slow.

<?php
// One helper for every call. The key comes from the environment, never from the code.
function arabybot(string $method, string $path, ?array $body = null): array
{
    $headers = ['Authorization: Bearer ' . getenv('ARABYBOT_API_KEY')];
    $options = [
        CURLOPT_CUSTOMREQUEST  => $method,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 20,
    ];
    if ($body !== null) {
        $headers[] = 'Content-Type: application/json';
        // JSON_UNESCAPED_UNICODE keeps Arabic values readable in your logs
        $options[CURLOPT_POSTFIELDS] = json_encode($body, JSON_UNESCAPED_UNICODE);
    }
    $options[CURLOPT_HTTPHEADER] = $headers;

    $ch = curl_init('https://app.arabybot.com/api/v1/' . ltrim($path, '/'));
    curl_setopt_array($ch, $options);
    $raw = curl_exec($ch);
    curl_close($ch);

    $answer = is_string($raw) ? json_decode($raw, true) : null;

    return is_array($answer) ? $answer : ['success' => false, 'error_code' => 'NETWORK'];
}

// 1) does the key work, and which numbers does it send from?
$me = arabybot('GET', 'me');

// 2) the approved templates — each comes with a ready request body in "example"
$templates = arabybot('GET', 'templates');

The templates answer carries, with each template, an example field holding the request body ready with the fields that very template needs. Start from it instead of guessing the number of variables.

Sending a template, and handling each answer

<?php
$sent = arabybot('POST', 'messages/template', [
    'to'        => '201012345678',             // with the country code
    'template'  => 'order_shipped',
    'language'  => 'ar',
    'variables' => ['Ahmed', '1042'],          // the template's {{1}}, {{2}} in order
    'reference' => 'order-1042-shipped',       // the same reference is never sent twice
]);

if (($sent['success'] ?? false) === true) {
    // "accepted" = WhatsApp took it, "queued" = it waits its turn and goes out by itself
    save_message_id($orderId, $sent['id']);
} elseif (in_array($sent['error_code'] ?? '', ['TRY_AGAIN', 'NETWORK'], true)) {
    retry_later($orderId);                     // same request, same reference
} elseif (($sent['error_code'] ?? '') === 'UNCERTAIN') {
    check_later($sent['id'] ?? null);          // ask GET messages/{id} before sending again
} else {
    error_log('ArabyBot ' . ($sent['error_code'] ?? '') . ': ' . ($sent['message_en'] ?? ''));
}

Three things in this code separate a trial from a system that runs every day. First the reference field: if your system repeats the request after a drop, the message will not arrive twice. Second, TRY_AGAIN is repeated with the same request, and UNCERTAIN is asked about before repeating. Third, the other codes — a template not yet approved, a customer who turned off marketing messages — are not helped by a retry, so log them and move on.

Every field, refusal code and limit, in the send API reference

Free text and the message status

<?php
// a free-text reply, only inside 24 hours of the customer's last message
$reply = arabybot('POST', 'messages/text', [
    'to'   => '201012345678',
    'text' => 'Your order 1042 is ready for pickup at the branch.',
]);

// where is message 512 now?  queued · accepted · sent · delivered · read · failed
$status = arabybot('GET', 'messages/512');
if (($status['status'] ?? '') === 'failed') {
    error_log('WhatsApp said: ' . ($status['error']['meta_code'] ?? '') . ' ' . ($status['error']['message'] ?? ''));
}

Free text is accepted only within 24 hours of the customer's last message — Meta's rule. Outside it, send a template. Ask for the status when you need it, such as writing «delivered» beside the order in your panel, and do not poll every message every second.

In Laravel

<?php
// config/services.php →  'arabybot' => ['key' => env('ARABYBOT_API_KEY')],

use Illuminate\Support\Facades\Http;

$sent = Http::withToken(config('services.arabybot.key'))
    ->timeout(20)
    ->post('https://app.arabybot.com/api/v1/messages/template', [
        'to'        => $order->customer_phone,
        'template'  => 'order_shipped',
        'language'  => 'ar',
        'variables' => [$order->customer_name, (string) $order->number],
        'reference' => 'order-' . $order->id . '-shipped',
    ])
    ->json();

Put this call inside a queued job rather than the controller, so your page answers the user at once and the job retries by itself on TRY_AGAIN. The reference built from the order id makes retrying the job safe.

Common PHP mistakes

The mistakeWhat happensThe fix
The key written in the codeIt leaks with the first copy of the repositoryAn environment variable; revoke the key if it shows up anywhere public
A number without the country codeBAD_PHONEStore numbers in international form from the start
Fewer variables than the templateBAD_VALUES with a list of what is missingRead example from the templates call
Sending from a page the user opensA page reload repeats the requestA background job, with a reference
No timeoutA hanging request stalls the pageCURLOPT_TIMEOUT

The same walkthrough in Python · And from Node.js · The OTP API

Frequently asked questions

1

Do I need a library or an SDK to send WhatsApp from PHP?

No. The API is ordinary HTTP calls with a JSON body; the cURL found on any PHP hosting is enough, or Laravel's Http client.

2

Does the code work on shared hosting?

Yes, as long as the cURL extension is on and the host allows outbound HTTPS, which is the case on most hosting. Put the key in the environment variables or in a file outside the public web folder.

3

Why do Arabic characters arrive garbled?

Usually because the file or the database is not in UTF-8. The API takes UTF-8 JSON and returns the same, so make sure the values you read from your database are in that encoding before you send them.

4

How do I send to hundreds of customers at once?

Send each message as its own request with a different reference, from a background job rather than a page a user opens. The API takes 600 requests a minute per key, and whatever exceeds your number's pace at WhatsApp waits in the queue and goes out by itself.

The ArabyBot teamOfficial Meta business partner in the Middle East 239 guides Figures from our platform All guides

How we write these guides Figures come from our platform and Meta's actual invoices, dated with their sample; we describe what works today, not what we plan, and say what we do not do and when someone else is the better choice. Reviewed by the ArabyBot team on 15 November 2026.

Share this guide
And you run all of it from your phoneEverything on this page is also run from the ArabyBot mobile app — replies, orders, campaigns and AI reports. Not a web dashboard only.

Try the agent before you pay anything

The agent that will answer your customers is the one answering you now: ask it from the chat button here or on WhatsApp. Your account is free with no credit card, and the free trial on your own number or page is activated after you contact our team on customer-service WhatsApp.