PHP code example of yii2vn / esms

1. Go to this page and download the library: Download yii2vn/esms 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/ */

    

yii2vn / esms example snippets


return [
    'components' => [
        'eSMS' => [
            'class' => 'yii2vn\esms\ESMS',
            'apiKey' => 'API KEY lấy từ ESMS',
            'secretKey' => 'SECRET KEY lấy từ ESMS'
        ]
    ]
]

$phone = '0909113911';
$esms = Yii::$app->eSMS;

if ($esms->sendSMS($phone, 'Xin chao')) {
    Yii::$app->session->setFlash('Success!');
} else {
    Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error);
}

$phones = ['0909113911', '0909911113', '0909123456'];
$esms = Yii::$app->eSMS;

if ($esms->batchSendSMS($phones, 'Xin chao')) {
    Yii::$app->session->setFlash('Success!');
} else {
    Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error);
}


$phones = [
      '0909113911' => 'Ngay mai hop luc 8h',
      '0909911113' => 'Thu 2 lam ca sang',
      '0909123456' => '01/05 duoc nghi lam'
];
$esms = Yii::$app->eSMS;

if ($esms->batchSendSMS($phones)) {
    Yii::$app->session->setFlash('Success!');
} else {
    Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error);
}


$phones = [
      '0909113911' => 'Ngay mai nhan vien se hop luc 8h! Kinh moi sep tham gia cuoc hop', // Số của sếp
      '0909911113', '0909123321', '0909963147'
];
$esms = Yii::$app->eSMS;

if ($esms->batchSendSMS($phones, 'Ngay mai moi nguoi du hop vao luc 8h! Co sep den du.')) {
    Yii::$app->session->setFlash('Success!');
} else {
    Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error);
}


$phone = '0909113911';
$apiCode = '58888'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại
$passCode = '12345'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại
$esms = Yii::$app->eSMS;

if ($esms->sendVoiceCall($phone, $apiCode, $passCode, 'Xin chao')) {
    Yii::$app->session->setFlash('Success!');
} else {
    Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error);
}

$phones = ['0909113911', '0909911113', '0909123456'];
$apiCode = '58888'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại
$passCode = '12345'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại
$esms = Yii::$app->eSMS;

if ($esms->batchSendVoiceCall($phones, $apiCode, $passCode, 'Xin chao')) {
    Yii::$app->session->setFlash('Success!');
} else {
    Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error);
}


$phones = [
      '0909113911' => 'Ngay mai hop luc 8h',
      '0909911113' => 'Thu 2 lam ca sang',
      '0909123456' => '01/05 duoc nghi lam'
];
$apiCode = '58888'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại
$passCode = '12345'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại
$esms = Yii::$app->eSMS;

if ($esms->batchSendVoiceCall($phones, $apiCode, $passCode)) {
    Yii::$app->session->setFlash('Success!');
} else {
    Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error);
}


$phones = [
      '0909113911' => 'Ngay mai nhan vien se hop luc 8h! Kinh moi sep tham gia cuoc hop', // Số của sếp
      '0909911113', '0909123321', '0909963147'
];
$apiCode = '58888'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại
$passCode = '12345'; // lấy trên hệ thống ESMS để xác định mẫu cuộc gọi thoại
$esms = Yii::$app->eSMS;

if ($esms->batchSendVoiceCall($phones, $apiCode, $passCode, 'Ngay mai moi nguoi du hop vao luc 8h! Co sep den du.')) {
    Yii::$app->session->setFlash('Success!');
} else {
    Yii::warning('Không thể gửi tin đến sđt: ' . $phone . ' Lỗi: ' . $esms->error);
}


$esms = Yii::$app->eSMS;
$smsId = '123-421-412412-123';
$statusData = $esms->getSendStatus($smsId);
var_dump($statusData);
// Kết quả:
// array(7) { ["CodeResponse"]=> string(3) "100" ["SMSID"]=> string(39) "123-421-412412-123" ["SendFailed"]=> int(0) ["SendStatus"]=> int(5) ["SendSuccess"]=> int(1) ["TotalReceiver"]=> int(1) ["TotalSent"]=> int(1) } 

$esms = Yii::$app->eSMS;
$smsId = '123-421-412412-123';
$statusData = $esms->getReceiverStatus($smsId);
var_dump($statusData);
// Kết quả:
// array(2) { ["CodeResult"]=> string(3) "100" ["ReceiverList"]=> array(1) { [0]=> array(3) { ["IsSent"]=> bool(true) ["Phone"]=> string(10) "0909113911" ["SentResult"]=> bool(true) } } } 

$esms = Yii::$app->eSMS;
var_dump($esms->getBalance());
// Kết quả:
// array(3) { ["Balance"]=> int(2293566) ["CodeResponse"]=> string(3) "100" ["UserID"]=> int(999999999999) } 

$esms = Yii::$app->eSMS;
var_dump($esms->getBalance());
// Kết quả:
// array(3) { ["Balance"]=> int(2293566) ["CodeResponse"]=> string(3) "100" ["UserID"]=> int(999999999999) } 
$esms->sendSMS("0909113911", "hello");
var_dump($esms->getBalance());
// Kết quả số dư vẫn như cữ:
// array(3) { ["Balance"]=> int(2293566) ["CodeResponse"]=> string(3) "100" ["UserID"]=> int(999999999999) } 
var_dump($esms->getBalance(true)); // kết quả sẽ thay đổi vì chúng ta lấy lại dữ liệu sau khi gửi tin => số tiền sẽ giảm


php composer.phar