PHP code example of salavati / sapak-sms-php

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

    

salavati / sapak-sms-php example snippets


use Sapak\Sms\SapakClient;

$apiKey = 'YOUR_API_KEY_HERE';
$client = new SapakClient($apiKey);

use Sapak\Sms\Exceptions\ValidationException;
use Sapak\Sms\Exceptions\AuthenticationException;
use Sapak\Sms\Exceptions\ApiException;

try {
    // Make an API call, e.g., $client->messages()->send(...)
    
} catch (ValidationException $e) {
    // 400 Bad Request or 422 Unprocessable
    // The request was malformed (e.g., missing 'text').
    echo "Validation Error: " . $e->getMessage();

} catch (AuthenticationException $e) {
    // 401 Unauthorized or 403 Forbidden
    // Your API key is likely invalid.
    echo "Auth Error: " . $e->getMessage();

} catch (ApiException $e) {
    // 500, 404, 429, or other generic errors
    echo "API Error: " . $e->getMessage();
}

// Returns a DTO, not a raw number
$creditDto = $client->account()->getCredit();

echo "Your credit is: " . $creditDto->credit . " Rials";

use Sapak\Sms\DTOs\Requests\SendMessage;

$message = new SendMessage(
    from: '985000...',
    to: ['98912...', '98913...'],
    text: 'This is a test message.'
);

// You can also schedule it (must be an ATOM/RFC3339 string)
$futureDate = (new \DateTime('+10 minutes'))->format(\DateTime::ATOM);
$scheduledMessage = new SendMessage(
    from: '985000...',
    to: ['98912...'],
    text: 'A scheduled message.',
    sendAt: $futureDate
);

$results = $client->messages()->send($message);

echo "Message submitted. ID: " . $results[0]->id;
echo "Status code: " . $results[0]->status;

use Sapak\Sms\DTOs\Requests\SendPeerToPeer;

$p2p_messages = [
    new SendPeerToPeer(
        sender: '985000...',
        recipient: '98912...',
        message: 'Hello, User 1.'
    ),
    new SendPeerToPeer(
        sender: '985000...',
        recipient: '98935...',
        message: 'Hello, User 2.'
    )
];

$results = $client->messages()->sendPeerToPeer($p2p_messages);

use Sapak\Sms\DTOs\Responses\SentMessageStatus;

$messageIds = [12345, 12346, 12347];

$statuses = $client->messages()->getStatuses($messageIds);

foreach ($statuses as $status) {
    echo "ID: " . $status->id . " has status code: " . $status->status . "\n";
    
    // Use the built-in constants for safe checking (No Magic Numbers!)
    if ($status->status === SentMessageStatus::STATUS_DELIVERED) {
        echo "Message {$status->id} was delivered!\n";
    }
    
    // Use the helper method for a human-readable text
    echo "Meaning: " . $status->getStatusText() . "\n";
}

use Sapak\Sms\DTOs\Requests\FindMessages;

$filters = new FindMessages(
    pageNumber: 1,
    pageSize: 20,
    fromDate: new \DateTime('-3 days'), // <-- Standard DateTime object
    toDate: new \DateTime('now')        // <-- Standard DateTime object
);

$receivedMessages = $client->messages()->find($filters);

// The DTO converts the API's Jalali string back to DateTimeImmutable!
foreach ($receivedMessages as $message) {
    echo "From: " . $message->fromNumber . "\n";
    echo "Text: " . $message->body . "\n";
    echo "Received At: " . $message->date->format('Y-m-d H:i:s') . "\n";
}
bash
composer