PHP code example of messageowl / msgowl-php

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

    

messageowl / msgowl-php example snippets


use MessageOwl\MessageOwl;

$client = new MessageOwl('your-api-key');

$client = new MessageOwl('your-api-key', useQueryAuth: true);

// Single recipient
$response = $client->message()
    ->to('9609848571')
    ->from('MyApp')
    ->body('Hello from MessageOwl!')
    ->send();

echo $response->id;      // int
echo $response->message; // "Message has been sent successfully."

// Multiple recipients
$client->message()
    ->to(['9609848571', '9609876543'])
    ->from('MyApp')
    ->body('Hello!')
    ->send();

// Latest 100 messages
$messages = $client->messages()->all();

foreach ($messages as $message) {
    echo $message->id;
    echo $message->smsHeader;
    echo $message->status;
    echo $message->createdAt;
    echo $message->body; // null if message.body.read scope is absent
}

$message = $client->messages()->find(8848);

echo $message->accountId;

foreach ($message->recipients as $recipient) {
    echo $recipient->phoneNumber;
    echo $recipient->deliveryStatus->name; // DeliveryStatus enum
    echo $recipient->smsStatus->name;      // SmsStatus enum
    echo $recipient->deliveredOn;
}

// Send OTP (code auto-generated if omitted)
$otp = $client->otp()->send('9609999999');
$otp = $client->otp()->send('9609999999', codeLength: 8);
$otp = $client->otp()->send('9609999999', code: '235311');

echo $otp->id;
echo $otp->phoneNumber;
echo $otp->timestamp;
echo $otp->messageId;

// Resend OTP
$otp = $client->otp()->resend('9609999999', id: $otp->id);

// Verify OTP
$result = $client->otp()->verify('9609999999', '235311');

echo $result->status ? 'Verified' : 'Invalid';

// List
$groups = $client->groups()->all();

// Fetch (>contacts as $contact) {
    echo $contact->name;
    echo $contact->number;
}

// Create
$group = $client->groups()->create('My Group');

// Update
$group = $client->groups()->update(22, 'New Name');

// Delete
$client->groups()->delete(22); // returns true

// List (paginated, 100 per page)
$list = $client->contacts()->all(page: 1);

echo $list->currentPage;
echo $list->nextPage;
echo $list->previousPage;

foreach ($list->contacts as $contact) {
    echo $contact->name;
    echo $contact->number;
}

// Create (optionally assign to groups by name)
$contact = $client->contacts()->create('John', '9609999999', ['My Group']);

foreach ($contact->groups as $group) {
    echo $group->id;
    echo $group->name;
}

// Update
$contact = $client->contacts()->update(1232, 'John Updated', '9609999999', ['My Group']);

// Delete
$client->contacts()->delete(1232); // returns true

$balance = $client->balance();

echo $balance->balance; // "130.6347" (string)

$senderIds = $client->senderIds();

foreach ($senderIds as $sender) {
    echo $sender->name;
    echo $sender->status;    // "approved" | "pending verification"
    echo $sender->purpose;   // nullable
    echo $sender->remarks;   // nullable
    echo $sender->handledAt; // nullable
}

use MessageOwl\Exceptions\AuthenticationException;
use MessageOwl\Exceptions\MethodNotAllowedException;
use MessageOwl\Exceptions\MessageOwlException;
use MessageOwl\Exceptions\NotFoundException;
use MessageOwl\Exceptions\RateLimitException;
use MessageOwl\Exceptions\RequestTimeoutException;
use MessageOwl\Exceptions\ServerException;
use MessageOwl\Exceptions\ValidationException;

try {
    $client->message()->to('9609848571')->from('App')->body('Hi')->send();
} catch (AuthenticationException $e) {
    // 401 — invalid API key
} catch (NotFoundException $e) {
    // 404 — resource not found
} catch (MethodNotAllowedException $e) {
    // 405
} catch (RequestTimeoutException $e) {
    // 408 or connection timeout
} catch (ValidationException $e) {
    // 422 — check $e->bulkLimit for bulk limit violations
    if ($e->bulkLimit !== null) {
        echo "Bulk limit: {$e->bulkLimit}";
    }
} catch (RateLimitException $e) {
    // 429 — retry after $e->retryAfter seconds
    echo "Retry after: {$e->retryAfter}s";
    echo "Limit: {$e->rateLimitLimit}";
    echo "Remaining: {$e->rateLimitRemaining}";
    echo "Reset at: {$e->rateLimitReset}";
} catch (ServerException $e) {
    // 5xx
} catch (MessageOwlException $e) {
    // catch-all for any other SDK error
}

use MessageOwl\Laravel\Facades\MessageOwl;

MessageOwl::message()->to('9609848571')->from('MyApp')->body('Hello!')->send();
MessageOwl::balance();
MessageOwl::senderIds();
bash
composer 
bash
php artisan vendor:publish --tag=messageowl-config