PHP code example of waapi / waapi-php-sdk

1. Go to this page and download the library: Download waapi/waapi-php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

waapi / waapi-php-sdk example snippets


use WaAPI\WaAPISdk\WaAPISdk;

$apiToken = 'xxxxxxxxxxxxxxxxxxxxxx';
$sdk = new WaAPISdk($apiToken);

$isAvailable = $sdk->isApiAvailable();

// Create a basic instance
$instance = $sdk->createInstance();
$instanceId = $instance->id;

// Create an instance with a name and webhook
$instance = $sdk->createInstance(
    name: 'My Instance',
    webhookUrl: 'https://my.url.com/webhook/handler',
    webhookEvents: ['message', 'ready', 'qr']
);

$instanceId = 10; //you need to know your instance id at this point
$instance = $sdk->getInstance($instanceId);

$instanceId = 10; //you need to know your instance id at this point

//if a subscribed event occurs, this url will be requested with the event data
//can also be null if you do not want to receive webhooks
$webhookUrl = '';
$subscribedEvents = ['', '']; //can also be null or an empty array

$sdk->updateInstance($instanceId, $webhookUrl, $subscribedEvents);

// You can also update the instance name
$sdk->updateInstance($instanceId, name: 'New Name');

//if you have an instance object, you can also use the following method
$instance = $sdk->getInstance($instanceId);
$instance->update($webhookUrl, $subscribedEvents);

$instanceId = 10; //you need to know your instance id at this point
$sdk->deleteInstance($instanceId);

//if you have an instance object, you can also use the following method to delete this instance
$instance = $sdk->getInstance($instanceId);
$instance->delete();

$instanceId = 10; //you need to know your instance id at this point
$response = $sdk->getInstanceClientQrCode($instanceId);

//if you have an instance object, you can also use the convenience method
$instance = $sdk->getInstance($instanceId);
$response = $instance->clientQrCode();

// The QR code as a base64 image string (data:image/png;base64,…).
// Set this string in the src attribute of a <img src=””> html tag.
$qrCode = $response->qrCode;

$instanceId = 10; //you need to know your instance id at this point
$response = $sdk->getInstanceClientStatus($instanceId);

//if you have an instance object, you can also use the convenience method
$instance = $sdk->getInstance($instanceId);
$response = $instance->clientStatus();

$instanceStatus = $response->instanceStatus;

$instanceId = 10; //you need to know your instance id at this point
$response = $sdk->getInstanceClientInfo($instanceId);

//if you have an instance object, you can also use the convenience method
$instance = $sdk->getInstance($instanceId);
$response = $instance->clientInfo();

//your public name of your WhatsApp profile (your name)
$displayName = $response->displayName;

//the connected WhatsApp phone number (your phone number)
$phoneNumber = $response->formattedNumber;

//profile image url of the connected WhatsApp phone number (your profile picture)
$profileUrl = $response->profilePicUrl;

//a unique identifier for your WhatsApp account / profile / phone number
$whatsAppId = $response->contactId;

$instanceId = 10;

// Send a text message
$result = $sdk->executeInstanceAction($instanceId, 'send-message', [
    'chatId' => '[email protected]',
    'message' => 'Hello from waapi!',
]);

// Send media
$result = $sdk->executeInstanceAction($instanceId, 'send-media', [
    'chatId' => '[email protected]',
    'mediaUrl' => 'https://example.com/image.png',
    'mediaCaption' => 'Check this out!',
]);

// Using the instance object
$instance = $sdk->getInstance($instanceId);
$result = $instance->executeClientAction('send-message', [
    'chatId' => '[email protected]',
    'message' => 'Hello!',
]);

use WaAPI\WaAPISdk\Resources\WebhookEvent;

$sdk->createInstance(
    name: 'My Instance',
    webhookUrl: 'https://my.url.com/webhook',
    webhookEvents: [
        WebhookEvent::MESSAGE,
        WebhookEvent::READY,
        WebhookEvent::QR,
        WebhookEvent::DISCONNECTED,
    ]
);

// Subscribe to all events
$sdk->createInstance(
    name: 'My Instance',
    webhookUrl: 'https://my.url.com/webhook',
    webhookEvents: WebhookEvent::ALL
);
shell
composer