PHP code example of kavenegar / php

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

    

kavenegar / php example snippets




use Kavenegar\KavenegarApi;
use Kavenegar\Exceptions\ApiException;
use Kavenegar\Exceptions\HttpException;

$api = new KavenegarApi("YOUR_API_KEY");

try {
    // Send a simple SMS
    $result = $api->send("10004346", "09123456789", "Hello from Kavenegar PHP SDK!");
    
    foreach ($result as $sms) {
        echo "✅ Sent! Message ID: " . $sms->messageid . "\n";
    }
} catch (ApiException $e) {
    echo "❌ API Error: " . $e->errorMessage() . "\n";
} catch (HttpException $e) {
    echo "❌ Network Error: " . $e->errorMessage() . "\n";
}

use Kavenegar\Models\SendRequest;

$req = new SendRequest();
$req->sender = "10004346";
$req->receptor = "09123456789";
$req->message = "OTP Code: 123456";
$req->policy = "policy_value"; // Optional policy

$result = $api->send($req);

use Kavenegar\Models\TemplateDto;
use Kavenegar\Enums\VerificationType;
use Kavenegar\Enums\VerificationUsageType;

$dto = new TemplateDto();
$dto->name = "otp-template-name";
$dto->textmessage = "Your verification code: %token%";
$dto->sourcetype = VerificationUsageType::MobileApplication;
$dto->sendmethod = VerificationType::Message;

$newTemplate = $api->addTemplate($dto);
bash
composer