PHP code example of msgly-official / msgly-sdk-php

1. Go to this page and download the library: Download msgly-official/msgly-sdk-php 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/ */

    

msgly-official / msgly-sdk-php example snippets




use Msgly\MsglyClient;
use Msgly\Exceptions\MsglyException;

$msgly = new MsglyClient([
    'base_url'      => 'https://api.msgly.id', // Optional, defaults to https://api.msgly.id
    'client_id'     => 'YOUR_CLIENT_ID',
    'client_secret' => 'YOUR_CLIENT_SECRET',
]);

try {
    $result = $msgly->message()->sendOTP([
        'xid'  => uniqid('otp_'), // Required: Unique transaction ID
        'to'   => '6281234567890', // Required: Destination number (62...)
        'code' => '482910',        // OTP Code to substitute into template
    ]);

    print_r($result);
} catch (MsglyException $e) {
    echo "Error sending OTP (Rotation): " . $e->getMessage();
}

try {
    $result = $msgly->message()->sendOTP([
        'xid'      => uniqid('otp_'),
        'to'       => '6281234567890',
        'template' => [
            'template_id' => 'c8f7a1b2-3c4d-5e6f-7a8b-9c0d1e2f3a4b',
            'params'      => ['482910'],
        ],
    ]);

    print_r($result);
} catch (MsglyException $e) {
    echo "Error sending OTP (Manual): " . $e->getMessage();
}

try {
    $notif = $msgly->message()->sendNotification([
        'xid'     => uniqid('notif_'),
        'to'      => '6281234567890',
        'message' => 'Halo Budi, paket pesanan #TRX-982 telah dikirimkan oleh kurir.',
    ]);
    print_r($notif);
} catch (MsglyException $e) {
    echo "Error sending notification: " . $e->getMessage();
}

try {
    $reminder = $msgly->message()->sendReminder([
        'xid'          => uniqid('rem_'),
        'to'           => '6281234567890',
        'message'      => 'Pengingat: Jadwal janji temu dokter Anda hari ini jam 15:00 WIB.',
        'scheduled_at' => date('c', strtotime('+1 day')),
    ]);
    print_r($reminder);
} catch (MsglyException $e) {
    echo "Error sending reminder: " . $e->getMessage();
}

try {
    // List templates (supports 'page', 'per_page', 'search')
    $templates = $msgly->template()->list(['search' => 'OTP', 'page' => 1, 'per_page' => 10]);
    print_r($templates);

    // Get specific template detail by ID
    if (!empty($templates['data']['data'])) {
        $templateId = $templates['data']['data'][0]['id'];
        $detail = $msgly->template()->getById($templateId);
        print_r($detail);
    }
} catch (MsglyException $e) {
    echo "Error fetching templates: " . $e->getMessage();
}
bash
composer