WhatsApp Cloud API PHP SDK

repository·main·Indexed 20 days ago

https://github.com/netflie/whatsapp-cloud-api

A PHP SDK for integrating with the WhatsApp Cloud API. It enables developers to send text, media, and template messages, manage business profiles, and handle interactive messages like lists and buttons. The library includes tools for uploading and downloading media, managing message templates, and processing incoming WhatsApp Webhook notifications. Requires PHP 7.4 or higher and CURL >= 7.19.4 with OpenSSL and zlib.

Tokens
7.8K
Snippets
31
Records
49
Agent score
67%

What's inside netflie/whatsapp-cloud-api

  1. Initialize the WhatsAppCloudApi client

    main

    To use the API, instantiate the WhatsAppCloudApi class with your configuration. You must provide your from_phone_number_id and your Facebook WhatsApp application access_token. If you intend to manage templates, you should also provide your business_id.

    use Netflie//WhatsAppCloudApi//WhatsAppCloudApi;
    
    $whatsapp_cloud_api = new WhatsAppCloudApi([
        'from_phone_number_id' => 'your-configured-from-phone-number-id',
        'access_token' => 'your-facebook-whatsapp-application-token',
    ]);
  2. Handle Webhooks (Verification and Notifications)

    main

    Webhook Verification

    When setting up your webhook in the Meta App dashboard, use the WebHook::verify method to validate the request.

    Reading Notifications

    Use the WebHook class to parse incoming JSON payloads from Meta. Use read() for a single notification or readAll() to handle batched notifications.

    use Netflie\WhatsAppCloudApi\WebHook;
    
    // Verification
    $webhook = new WebHook();
    echo $webhook->verify($_GET, "<your-verify-token>");
    
    // Reading notifications
    $payload = json_decode(file_get_contents('php://input'), true);
    $webhook = new WebHook();
    $notification = $webhook->read($payload);
  3. Upgrade from 3.x to 4.x: `sendContact` parameter order change

    main

    In version 4.x, the sendContact() method has changed its parameter order. The BSUID recipient argument now comes before the variadic phone arguments.

    If you were previously passing a Phone as the third argument, you must now explicitly pass null or the BSUID recipient first to maintain correct argument positioning.

    // Sending to a standard phone number
    $whatsapp_cloud_api->sendContact(to: $to, name: $name, recipient: null, phone: $phone);
    
    // Sending only to a BSUID recipient
    $whatsapp_cloud_api->sendContact(to: null, name: $name, recipient: $recipient_bsuid, phone: $phone);
  4. Install the WhatsApp Cloud API via Composer

    main

    Install the library using Composer to integrate the WhatsApp Cloud API into your PHP project.

    composer require netflie/whatsapp-cloud-api
  5. Upgrade from 3.x to 4.x: BSUID recipient support

    main

    In version 4.x, all send* methods that post to /messages support an optional ?string $recipient = null argument for BSUID (Business Solution User ID) recipients.

    • You can send a message using to, recipient, or both.
    • If both are provided, the WhatsApp Cloud API gives precedence to to.
    • The base message model now requires at least one of to or recipient to be present.
    // Sending with BSUID
    $whatsapp_cloud_api->sendTextMessage(to: null, text: 'Hello from BSUID', preview_url: false, recipient: 'US.13491208655302741918');
  6. Upgrade from 2.x to 3.x: `MessageNotificationFactory` is now `final`

    main
    The class Netflie\WhatsAppCloudApi\WebHook\Notification\MessageNotificationFactory is now marked as final. If your code extends this class, you must refactor it to use composition instead of inheritance.
  7. Handle API responses and errors

    main

    The WhatsAppCloudApi methods return a Response object on success. If the WhatsApp servers return an error, a \Netflie\WhatsAppCloudApi\Response\ResponseException is thrown. You can catch this exception to inspect the actual response returned by Meta.

    try {
        $response = $whatsapp_cloud_api->sendTextMessage('34676104574', 'Hello');
    } catch (\Netflie\WhatsAppCloudApi\Response\ResponseException $e) {
        print_r($e->response()); // Inspect the Meta server response
    }