PHP code example of sashalenz / dinstar-api

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

    

sashalenz / dinstar-api example snippets


use Sashalenz\DinstarApi\Facades\DinstarApi;

$response = DinstarApi::sendSms('Hello!', [['number' => '+380501234567']]);
$response->get('task_id'); // 7

DinstarApi::for('https://10.0.0.5', 'admin', 'secret')
    ->sendSms('Hi', [['number' => '+380501234567']]);

use Sashalenz\DinstarApi\Enums\SmsEncoding;

DinstarApi::sendSms(
    text: '#param#',
    recipients: [
        ['number' => '+380501234567', 'text_param' => ['Bob'], 'user_id' => 1],
        ['number' => '+380939876543', 'text_param' => ['Alice'], 'user_id' => 2],
    ],
    ports: [2, 3],                 // optional
    encoding: SmsEncoding::UNICODE, // or SmsEncoding::GSM_7BIT
    requestStatusReport: true,
);

DinstarApi::querySmsResult(userIds: [1, 2]);
DinstarApi::querySmsDeliverStatus(numbers: ['+380501234567']);
DinstarApi::querySmsInQueue();
DinstarApi::queryIncomingSms(flag: \Sashalenz\DinstarApi\Enums\IncomingSmsFlag::ALL);
DinstarApi::stopSms(taskId: 7);

use Sashalenz\DinstarApi\Enums\UssdCommand;

DinstarApi::sendUssd(ports: [1, 2], text: '*125#');
DinstarApi::sendUssd(ports: [1], text: '', command: UssdCommand::CANCEL);
DinstarApi::queryUssdReply([1, 2]);

use Sashalenz\DinstarApi\Enums\PortAction;
use Sashalenz\DinstarApi\Enums\PortInfoType;

DinstarApi::getPortInfo(
    infoTypes: [PortInfoType::TYPE, PortInfoType::IMEI, PortInfoType::SIGNAL],
    ports: [1, 2, 3],
);

DinstarApi::setPortInfo(port: 1, action: PortAction::POWER, param: 'off');
DinstarApi::setPortInfo(port: 1, action: PortAction::RESET);
DinstarApi::setPortInfo(port: 1, action: PortAction::SLOT, param: '2');

use Sashalenz\DinstarApi\Enums\CallForwardType;

DinstarApi::setCallForward(8, CallForwardType::UNCONDITIONAL, '15013828917');
DinstarApi::checkCallForward(8);
DinstarApi::getCallForward(8);

DinstarApi::getCdr(
    ports: [2, 3],
    timeAfter: '2024-01-01 00:00:00',
    timeBefore: now(),
);

use Sashalenz\DinstarApi\Enums\StkAction;

DinstarApi::getStkView(0);
DinstarApi::stkGo(port: 7, item: 1);
DinstarApi::stkGo(port: 7, action: StkAction::CANCEL);
DinstarApi::getStkCurrFrameIndex(0);

DinstarApi::getStatus(); // ['performance']

use Illuminate\Notifications\Notification;
use Sashalenz\DinstarApi\Notifications\DinstarSmsChannel;
use Sashalenz\DinstarApi\Notifications\DinstarSmsMessage;

class OrderShipped extends Notification
{
    public function via(object $notifiable): array
    {
        return [DinstarSmsChannel::class]; // or 'dinstar-sms'
    }

    public function toDinstarSms(object $notifiable): DinstarSmsMessage
    {
        return DinstarSmsMessage::create('Your order has shipped!')
            ->ports([1, 2])         // optional
            ->withoutDeliveryReport() // optional
            ->userId($this->order->id); // optional
    }
}

public function toDinstarSms(object $notifiable): string
{
    return 'Your code is 12345';
}

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForDinstarSms($notification)
    {
        return $this->phone;
    }
}

Notification::route('dinstar-sms', '+380501234567')
    ->notify(new OrderShipped($order));

use Sashalenz\DinstarApi\PushEvent;

Route::post('/dinstar/push', function (Request $request) {
    $event = PushEvent::fromPayload($request->getContent());

    match (true) {
        $event->isIncomingSms()    => IncomingSms::ingest($event->sn, $event->data),
        $event->isSmsResult()      => SmsResult::ingest($event->sn, $event->data),
        $event->isDeliverStatus()  => DeliverStatus::ingest($event->sn, $event->data),
        $event->isCdr()            => Cdr::ingest($event->sn, $event->data),
        default                    => null,
    };

    return response()->noContent();
});

use Sashalenz\DinstarApi\Exceptions\DinstarException;

try {
    DinstarApi::sendSms('Hi', [['number' => '+380501234567']]);
} catch (DinstarException $e) {
    // $e->getCode()    -> the gateway error_code (550, 413, 500, …)
    // $e->endpoint     -> the API path that failed
    // $e->payload      -> the full decoded response (when available)
}
bash
php artisan vendor:publish --tag="dinstar-api-config"