PHP code example of texhub / oson-sms

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

    

texhub / oson-sms example snippets


use TexHub\OsonSms\OsonSms;

$oson = OsonSms::make(
    login: 'YOUR_LOGIN',
    token: 'YOUR_TOKEN',
    sender: 'YOUR_SENDER',
);

$response = $oson->send('992900123456', 'Привет! Это тест OsonSMS.');

echo $response->messageId;  // msg_id
echo $response->txnId;      // txn_id

use TexHub\OsonSms\Requests\SmsMessage;

$oson->send(
    SmsMessage::make('992900123456', 'Ваш код: 1234')
        ->from('MyShop')        // override the default sender
        ->txnId('order-42')     // idempotency key (auto-generated otherwise)
);

$results = $oson->sms()->sendMany([
    SmsMessage::make('992900000001', 'Сообщение 1'),
    SmsMessage::make('992900000002', 'Сообщение 2'),
]);

foreach ($results as $r) {
    if ($r['ok']) {
        echo "sent: {$r['response']->messageId}\n";
    } else {
        echo "failed: {$r['error']->getMessage()}\n";
    }
}

$oson = OsonSms::fromArray([
    'login' => '...', 'token' => '...', 'sender' => '...',
    'hash_secret' => 'YOUR_API_HASH',
]);

$oson = OsonSms::fromArray([
    'login' => '...', 'sender' => '...', 'hash_secret' => 'YOUR_API_HASH',
]);

use TexHub\OsonSms\Exceptions\ApiException;
use TexHub\OsonSms\Exceptions\TransportException;
use TexHub\OsonSms\Exceptions\OsonSmsException;

try {
    $oson->send('992900123456', 'Hi');
} catch (ApiException $e) {
    $e->errorCode;   // error.code
    $e->apiMessage;  // error.msg
    $e->errorType;   // error.error_type
} catch (TransportException $e) {
    // network failure
} catch (OsonSmsException $e) {
    // base type — also covers invalid responses
}

use TexHub\OsonSms\Laravel\OsonSms;
use TexHub\OsonSms\Requests\SmsMessage;

OsonSms::send('992900123456', 'Привет из Laravel!');

OsonSms::send(
    SmsMessage::make('992900123456', 'Ваш код: 1234')->txnId('order-'.$order->id)
);

public function notify(\TexHub\OsonSms\OsonSms $oson) { /* ... */ }

use TexHub\OsonSms\OsonSms;
use TexHub\OsonSms\Config;
use TexHub\OsonSms\Tests\Support\FakeTransport;

$transport = (new FakeTransport())->willReturnJson(['msg_id' => '1', 'txn_id' => 'a']);
$oson = new OsonSms(new Config('login', 'token', 'Sender'), $transport);

$oson->send('992900123456', 'Hi');
// assert on $transport->lastQuery / lastHeaders / lastUrl

str_hash = SHA256( txn_id ; login ; sender ; phone_number ; hash_secret )
bash
php artisan vendor:publish --tag=oson-sms-config

src/
├── OsonSms.php              # entry point — sms() / send()
├── Config.php               # immutable configuration
├── Signature.php            # optional SHA-256 str_hash signer
├── Http/                    # Transport interface, CurlTransport, Response
├── Requests/SmsMessage.php  # fluent message builder
├── Clients/SmsClient.php    # send() / sendMany()
├── Exceptions/              # ApiException, TransportException, …
└── Laravel/                 # ServiceProvider + Facade