woenel/laravel-txtbox

TxtBox API for Laravel.

v2.0.4 2022-09-16 11:16 UTC

This package is auto-updated.

Last update: 2024-04-16 14:57:25 UTC


README

TxtBox SMS API for Laravel.

Packagist Version Packagist

What is TxtBox

Txtbox lets you send an SMS with a single line of code. No complicated setup, no dealing with telecom protocols and procedures.

For more info, you can visit their website at: https://www.txtbox.com

Requirements

  • PHP: 7.2.x and up or 8.0.x
  • Laravel: 8.x.x or 9.x.x (For Laravel 7.x.x, use version 1.x.x)
  • TxtBox API Key

Installation

Install using Composer

For Laravel 8:
$ composer require woenel/laravel-txtbox

For Laravel 7:
$ composer require woenel/laravel-txtbox "^1.3"

Publish the config file named txtbox.php and set the TxtBox API key.

$ php artisan vendor:publish --provider="Woenel\TxtBox\TxtBoxServiceProvider"

Usage

Send a Message

These are the three methods you can use to send a message:

1. Using facade
use Woenel\TxtBox\Facades\TxtBox;

$result = TxtBox::to('09123456789')->message('Hello World!')->send();

if($result->success) {
  echo $result->message;
} else {
  echo $result->message;
}
2. Instantiating class (chaining)
use Woenel\TxtBox\TxtBox;

$txtbox = new TxtBox;

$result = $txtbox->to('09123456789')->message('Hello World!')->send();

if($result->success) {
  echo $result->message;
} else {
  echo $result->message;
}
3. Instantiating class (one-by-one)
use Woenel\TxtBox\TxtBox;

$txtbox = new TxtBox;

$txtbox->to = '09123456789';
$txtbox->message = 'Hello World!';
$result = $txtbox->send();

if($result->success) {
  echo $result->message;
} else {
  echo $result->message;
}

Success and Failure returns

TxtBox Laravel API returns object-type values.

Success

Message successfully sent to 09XXXXXXXXX
This happens when you successfully sent a message.

{
  "success": true,
  "message": 'Message successfully sent to 09XXXXXXXXX'
}

Failures

Invalid Token
This happens when you did not provide a valid API key. API key can be modified in txtbox.php file under config folder.

{
  "success": false,
  "message": 'Invalid Token'
}

Unprocessable Entity
This happens when you did not enter a value. Number to() and Message message() are both required entities.

{
  "success": false,
  "message": 'Unprocessable Entity',
  "errors": {
    "number": [
      0 => 'The message field is required.'
    ],
    "message": [
      0 => 'The number field is required.'
    ]
  }
}

Insufficient credits. Please buy more credit to use this service.
This happens when you already used all of your SMS credits.

{
  "success": false,
  "message": 'Insufficient credits. Please buy more credit to use this service.'
}