PHP code example of terenaa / sms-gateway

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

    

terenaa / sms-gateway example snippets




use terenaa\SmsGateway\SmsGateway;

try {
    $sms = new SmsGateway();
    $sms->send('123456789', 'The message');
} catch (SmsGatewayException $e) {
    echo $e->getMessage();
}



use terenaa\SmsGateway\SmsGateway;

try {
    $sms = new SmsGateway();
    $sms->sendMultiple(array('123456789', '234567891'), 'The message');
} catch (SmsGatewayException $e) {
    echo $e->getMessage();
}

// /home/USERNAME/Scripts/SMSGateway/sms.php

aa\SmsGateway\SmsGatewayException;

$opts = getopt('p:m:s::b::', array('phone:', 'msg:', 'sig::', 'phoneback::'));
$phone = fetchOpt($opts, array('p', 'phone'));
$msg = fetchOpt($opts, array('m', 'msg'));
$sig = fetchOpt($opts, array('s', 'sig'));
$phoneback = fetchOpt($opts, array('b', 'phoneback'));

if (!$phone || !$msg) {
    echo "Usage: sms --phone=<phone> --msg=<message> [--sig=<signature> [--phoneback=<phone back>]]\n";
    exit;
}

try {
    $sms = new SmsGateway();
    
    if ($sms->send($phone, $msg, $sig, $phoneback)) {
        echo "Message has been sent.\n";
    } else {
        echo "Something went wrong...\n";
    }
} catch (SmsGatewayException $e) {
    echo $e->getMessage();
}

function fetchOpt($opts, array $names)
{
    foreach ($names as $name) {
        if (isset($opts[$name])) {
            return $opts[$name];
        }
    }
    
    return null;
}



use terenaa\SmsGateway\Contact;
use terenaa\SmsGateway\SmsGateway;
use terenaa\SmsGateway\SmsGatewayException;

$opts = getopt('p:m:s::b::c::', array('phone:', 'msg:', 'sig::', 'phoneback::', 'contact::', 'save'));
$phone = fetchOpt($opts, array('p', 'phone'));
$msg = fetchOpt($opts, array('m', 'msg'));
$sig = fetchOpt($opts, array('s', 'sig'));
$phoneback = fetchOpt($opts, array('b', 'phoneback'));
$name = fetchOpt($opts, array('c', 'contact'));
$save = fetchOpt($opts, array('save'));

if ((!$phone && !$name) || !$msg) {
    echo "Usage:\n\n"
        . "    sms -p|-c phone number or contact's name -m message [-s signature]\n\t[-b phoneback] [-c contact name] [-s]\n\n"
        . "OPTIONS\n"
        . "\t-p, --phone=NUMBER\n"
        . "\t\trecipient's phone number, , $phoneback)) {
        echo "Message has been sent.\n";
    } else {
        echo "Something went wrong...\n";
    }

    if ($save && $name && $phone) {
        $saved = $contact->create($phone, $name);

        if ($saved) {
            echo "Contact '{$name}' has been saved.\n";
        } else {
            echo "Cannot save contact.\n";
        }
    }
} catch (SmsGatewayException $e) {
    echo $e->getMessage();
}

function fetchOpt($opts, array $names)
{
    foreach ($names as $name) {
        if (isset($opts[$name])) {
            return $opts[$name] ? $opts[$name] : true;
        }
    }

    return null;
}
bash
alias sms="/home/USERNAME/Scripts/SMSGateway/sms.php"