PHP code example of envoisms / envoisms-php

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

    

envoisms / envoisms-php example snippets




nvoiSMS\Client;

$client = new Client(getenv('ENVOISMS_API_KEY'));

$response = $client->send([
    'to' => '+212600000000',
    'message' => 'Votre code de vérification est 492018',
    'from' => 'MonBusiness', // validated Sender ID, or omit to use your default
]);

echo 'Message ID: ' . $response['id']; // poll it with getMessage(), match it in webhooks

// Reply to a customer who wrote to your number in the last 24 h
$response = $client->send([
    'to' => '+212600000000',
    'message' => 'Bonjour ! Votre commande #89240 a été expédiée.',
    'channel' => 'whatsapp',
]);

// Start the conversation yourself: approved template, any time
$response = $client->send([
    'to' => '+212600000000',
    'message' => 'Votre commande #89240 a été expédiée.', // shown in your history; the template body is what goes out
    'channel' => 'whatsapp',
    'template' => ['name' => 'order_shipped', 'language' => 'fr', 'variables' => ['89240']],
]);

$response = $client->send([
    'to' => '+212600000000',
    'message' => 'Votre chauffeur arrive dans 2 minutes.',
    'channel' => 'whatsapp',
    'cascade' => true,
]);

// 1. Send OTP (channel defaults to sms; pass channel => 'whatsapp' to send over WhatsApp instead)
$otpResponse = $client->sendOtp([
    'to' => '+212600000000',
    'brand' => 'MonBusiness',
    'code_length' => 6,
    'expiry' => 600, // seconds
]);

$sessionId = $otpResponse['session_id'];

// 2. Check the code the user typed in
$verifyResult = $client->checkOtp($sessionId, '492018');

if (!empty($verifyResult['verified'])) {
    echo "OTP verified successfully!";
}

// Optional: inspect a session's status without consuming an attempt
$session = $client->getOtpSession($sessionId);

$client->sendBulk([
    'messages' => [
        ['to' => '+212600000001', 'message' => 'Promo -20% ce week-end'],
        ['to' => '+212600000002', 'message' => 'Promo -20% ce week-end'],
    ],
    'from' => 'MonBusiness',
]);

$status = $client->getMessage('msg_123');
$recent = $client->listMessages(50, 0);

use EnvoiSMS\Client;

$rawBody = file_get_contents('php://input'); // raw request body, not the parsed array
$isValid = Client::verifyWebhookSignature(
    $rawBody,
    $_SERVER['HTTP_X_ENVOISMS_SIGNATURE'],
    getenv('ENVOISMS_WEBHOOK_SECRET')
);

$balance = $client->getBalance();
$packs = $client->listPacks();
$paymentMethods = $client->listPaymentMethods();

$client->createTopup(['amount_mad' => 200, 'payment_method' => 'stripe']);

$stats = $client->analytics(30);
$newKey = $client->createApiKey(['name' => 'Server key']);

$client->createOptout('+212600000000');

use EnvoiSMS\Client;
use EnvoiSMS\EnvoiSMSError;

$client = new Client(getenv('ENVOISMS_API_KEY'), maxRetries: 3, timeoutSeconds: 20);

try {
    $client->send(['to' => '+212600000000', 'message' => 'Test']);
} catch (EnvoiSMSError $e) {
    echo "Send failed ({$e->statusCode} {$e->errorCode}): {$e->getMessage()}";
}
bash
composer