PHP code example of bayurifkialghifari / waxum-php-client

1. Go to this page and download the library: Download bayurifkialghifari/waxum-php-client 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/ */

    

bayurifkialghifari / waxum-php-client example snippets


return [
    'base_url' => env('WAXUM_BASE_URL', 'http://localhost:3451'),
    'token'    => env('WAXUM_TOKEN'),
];

use Bayurifkialghifari\WaxumApi\Facades\WaxumApi;
use Bayurifkialghifari\WaxumApi\DTOs\Session\CreateSessionRequest;
use Bayurifkialghifari\WaxumApi\DTOs\Common\SendTextRequest;

// Create a WhatsApp session using DTO
$session = WaxumApi::session()->create(new CreateSessionRequest(
    name: 'Support Session',
));
echo $session->id;

// Send a text message using DTO
$response = WaxumApi::message()->sendText('session-id-123', new SendTextRequest(
    to: '[email protected]',
    text: 'Hello from Waxum PHP Client! 👋',
));

echo $response->messageId; // Typed property from SendResponse DTO

// Arrays are also supported for quick calls
$response = WaxumApi::message()->sendText('session-id-123', [
    'to' => '[email protected]',
    'text' => 'Hello from array request!',
]);

use Bayurifkialghifari\WaxumApi\DTOs\Session\CreateSessionRequest;
use Bayurifkialghifari\WaxumApi\DTOs\Common\ConnectRequest;

// Create session
$session = WaxumApi::session()->create(new CreateSessionRequest(name: 'My Session'));
echo $session->id;

// Connect session
$result = WaxumApi::session()->connect('session-123', new ConnectRequest(subscribe: ['Message']));
echo $result->message;

// Get status
$status = WaxumApi::session()->getStatus('session-123');
echo $status->isLoggedIn; // bool
echo $status->socketAlive; // bool
echo $status->paused;      // bool

// Pause / resume event processing without disconnecting
WaxumApi::session()->pause('session-123');
WaxumApi::session()->resume('session-123');

// Force an app-state resync
use Bayurifkialghifari\WaxumApi\DTOs\Session\AppStateResyncMode;

WaxumApi::session()->resyncAppState('session-123', ['critical_block'], AppStateResyncMode::SNAPSHOT);

// Fleet-wide operations
WaxumApi::session()->disconnectAll();
WaxumApi::session()->reconnectAll();
$hits = WaxumApi::session()->search('support');

// Export / import a session (ZIP)
WaxumApi::session()->export('session-123')->saveAs(storage_path('backup.zip'));
WaxumApi::session()->import('session-123', storage_path('backup.zip'));

use Bayurifkialghifari\WaxumApi\DTOs\Common\SendTextRequest;
use Bayurifkialghifari\WaxumApi\DTOs\Common\SendImageRequest;

// Send text
$response = WaxumApi::message()->sendText('session-123', new SendTextRequest(
    to: '[email protected]',
    text: 'Hello World!',
));
echo $response->messageId;

// Send image
$imageResponse = WaxumApi::message()->sendImage('session-123', new SendImageRequest(
    to: '[email protected]',
    image: 'https://example.com/image.jpg',
    caption: 'Awesome picture',
));

use Bayurifkialghifari\WaxumApi\DTOs\Group\CreateGroupRequest;

// Create group
$group = WaxumApi::group()->create('session-123', new CreateGroupRequest(
    subject: 'Development Team',
    participants: ['[email protected]'],
));

use Bayurifkialghifari\WaxumApi\DTOs\Blast\CreateBlastRequest;

// Create blast job
$blast = WaxumApi::blast()->create('session-123', new CreateBlastRequest(
    name: 'Promo Blast',
    recipients: ['[email protected]'],
    message: 'Check out our latest offer!',
));

$upload = WaxumApi::media()->upload(
    sessionId: 'session-123',
    filePath: storage_path('app/photo.jpg'),
    mediaType: 'image',
    mimetype: 'image/jpeg',
);
echo $upload->url;

use Bayurifkialghifari\WaxumApi\DTOs\Webhook\RegisterWebhookRequest;

// Register webhook
$webhook = WaxumApi::webhook()->register('session-123', new RegisterWebhookRequest(
    url: 'https://example.com/webhook',
    events: ['message', 'presence'],
));

// Dead-letter queue: inspect and replay failed deliveries
$dlq = WaxumApi::webhook()->listDlq('session-123');
WaxumApi::webhook()->replayDlq('session-123', $dlq->entries[0]->id);

use Bayurifkialghifari\WaxumApi\WebhookSignature;

public function handle(Request $request)
{
    if (! WebhookSignature::fromRequest($request, config('services.waxum.webhook_secret'))) {
        abort(401);
    }

    // ... process $request->json()
}

WebhookSignature::verify(
    rawBody: $rawBody,
    timestamp: $timestampHeader,
    signatureHeader: $signatureHeader,
    secret: $secret,
    toleranceSeconds: 300,
);

$info  = WaxumApi::fleet()->info();   // version + geo location
$stats = WaxumApi::fleet()->stats();  // session/webhook counters, uptime
$ready = WaxumApi::fleet()->ready();  // readiness probe with per-session state

WaxumApi::fleet()->reenableAllWebhooks(); // close all open circuit breakers

use Bayurifkialghifari\WaxumApi\DTOs\Token\MintTokenRequest;

$token = WaxumApi::tokens()->mint(new MintTokenRequest(
    name: 'blast-worker',
    sessionIds: ['session-123'],
    expiresInHours: 720,
));
echo $token->token;
$all   = WaxumApi::tokens()->list();
WaxumApi::tokens()->revoke($token->id);

WaxumApi::tags()->add('session-123', 'production');
WaxumApi::tags()->setTags('session-123', ['production', 'billing']);
$tags = WaxumApi::tags()->forSession('session-123');

use Bayurifkialghifari\WaxumApi\DTOs\Labels\CreateLabelRequest;

WaxumApi::labels()->createLabel('session-123', new CreateLabelRequest(
    labelId: '1',
    name: 'Lead',
    colorId: 3,
));

WaxumApi::labels()->addChatToLabel('session-123', '1', '[email protected]');

$catalog = WaxumApi::business()->catalog('session-123', '[email protected]');

$bots    = WaxumApi::bots()->bots('session-123');
$capping = WaxumApi::bots()->capping('session-123');

   // 1.x (never worked against the server)
   WaxumApi::media()->upload($sessionId, ['file' => $base64], $token);

   // 2.0
   WaxumApi::media()->upload($sessionId, '/path/to/file.jpg', 'image', 'image/jpeg', $token);
   
bash
composer 
bash
php artisan vendor:publish --tag="waxum-config"