PHP code example of ay4t / php-watzap-id

1. Go to this page and download the library: Download ay4t/php-watzap-id 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/ */

    

ay4t / php-watzap-id example snippets


use Dotenv\Dotenv;
use Ay4t\WatzapId\Config\WatzapConfig;
use Ay4t\WatzapId\WatzapClient;

// Load environment variables
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

// Inisialisasi konfigurasi
$config = new WatzapConfig();
$config->setApiKey($_ENV['API_KEY'])
       ->setNumberKey($_ENV['NUMBER_KEY']);

// Buat instance WatzapClient
$watzap = new WatzapClient($config);

try {
    $status = $watzap->checkApiStatus();
    print_r($status);
} catch (ApiException $e) {
    echo "Error: " . $e->getMessage();
}

try {
    $validation = $watzap->validateNumber('628123456789');
    print_r($validation);
} catch (ApiException $e) {
    echo "Error: " . $e->getMessage();
}

try {
    $message = $watzap->sendMessage(
        '628123456789',
        'Halo! Ini adalah pesan dari WatzapClient.',
        true // tunggu sampai pesan terkirim
    );
    print_r($message);
} catch (ApiException $e) {
    echo "Error: " . $e->getMessage();
}

try {
    $imageMessage = $watzap->sendImage(
        '628123456789',
        'https://example.com/image.jpg',
        'Caption untuk gambar'
    );
    print_r($imageMessage);
} catch (ApiException $e) {
    echo "Error: " . $e->getMessage();
}

// Ambil daftar grup
try {
    $groups = $watzap->getGroups();
    print_r($groups);
} catch (ApiException $e) {
    echo "Error: " . $e->getMessage();
}

// Kirim pesan ke grup
try {
    $groupMessage = $watzap->sendGroupMessage(
        '[email protected]',
        'Halo semua! Ini adalah pesan grup.'
    );
    print_r($groupMessage);
} catch (ApiException $e) {
    echo "Error: " . $e->getMessage();
}

// Kirim gambar ke grup
try {
    $groupImage = $watzap->sendGroupImage(
        '[email protected]',
        'https://example.com/image.jpg',
        'Caption untuk gambar grup'
    );
    print_r($groupImage);
} catch (ApiException $e) {
    echo "Error: " . $e->getMessage();
}

try {
    $response = $watzap->sendMessage('628123456789', 'Pesan');
} catch (ApiException $e) {
    echo "Error Message: " . $e->getMessage() . "\n";
    echo "HTTP Status: " . $e->getHttpStatusCode() . "\n";
    echo "Response Body: " . $e->getResponseBody() . "\n";
}
bash
composer