mane-olawale/termii
Composer 安装命令:
composer require mane-olawale/termii
包简介
A simple Object Oriented PHP Client for Termii SMS API.
README 文档
README
Termii Client
A simple Object Oriented PHP Client for Termii SMS API.
Uses Termii API.
Requirements
- PHP >= 7.2
- Guzzlehttp ~6|~7
Installation
Via Composer.
PHP 7.2+:
composer require mane-olawale/termii
You now have Termii Client installed in vendor/mane-olawale/termii
And an handy autoload file to include in your project in vendor/autoload.php
Basic usage of Termii client
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; // Create a new Client instance $client = new Client('{api_key}'); // Create a new Client instance and set options $client = new Client('{api_key}', [ 'sender_id' => 'Olawale', 'channel' => 'generic', "attempts" => 10, "time_to_live" => 30, "length" => 6, "placeholder" => '{token}', 'pin_type' => 'ALPHANUMERIC', 'message_type' => 'ALPHANUMERIC', 'type' => 'plain', ]); $client->sms->send('2347041945964', 'Hello World!'); // You can change any option later $client->fillOptions([ "attempts" => 5, "time_to_live" => 20, "length" => 4, "placeholder" => '{pin}', ]);
Enhanced http response
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); $response = $client->sms->send('2347041945964', 'Hello World!'); // Check for success if ($response->successful()) { // Do something } // Check for failed response if ($response->failed()) { // Do something }
Error handling
<?php // Run on success $response->onSuccess(function ($response) { // Do something }); // Run on error $response->onError(function ($response) { // Do something });
You can still treat the response as array
$message_id = $response['message_id'];
You can use foreach directly on list responses e.g history, sender list etc.
<?php foreach ($response as $sender) { // Work with each sender data }
Note There are more helper methods to the response. Check out the full documentation here.
Sender
Getting Sender ID list
Uses Sender ID.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); $client->sender->list();
Note We didn`t add the Sender id and channel becuase they are optional and they can always be passed later on the client object or the SMS API handler.
Request Sender ID
Uses Request Sender ID.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); $client->sender->request('Olawale', 'Friendship based Notifications', 'Mane Olawale');
SMS
Send Message
Uses Switch - Messaging.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}', [ 'sender_id' => '{sender_id}', 'channel' => '{channel}', ]); return $client->sms->send('2347041945964', 'Testing');
Custom Sender ID or Channel
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}', [ 'sender_id' => '{sender_id}', 'channel' => '{channel}', ]); return $client->sms->send('2347041945964', 'Hello World', 'Olawale', 'generic'); // OR probably omit sender id or channel return $client->sms->send('2347041945964', 'Hello World', null, 'generic');
You can fetch the message resource right after.
$client = new Client('{api_key}'); $response = $client->sms->send('2347041945964', 'Testing'); $message = $response->message();
Send Number
Uses Switch - Number.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); return $client->sms->number('2347041945964', 'Hello World');
Template
Uses Switch - Template.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); return $client->sms->template('2347041945964', '{template_id}', [ 'product_name' => 'Termii', 'otp' => '120435', 'expiry_time' => '10 minutes' ], '{device_id}');
Token
Send Token
Uses Send Token.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}', [ 'sender_id' => '{sender_id}', 'channel' => '{channel}', ]); // You can choose to omit the pin options if you have set them when creating the client instance return $client->token->sendToken('2347041945964', '{token} is your friendship verification token', [ "attempts" => 10, "time_to_live" => 30, "length" => 6, "placeholder" => '{token}', 'type' => 'NUMERIC', ]);
Custom Sender ID or Channel
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}', [ 'sender_id' => '{sender_id}', 'channel' => '{channel}', ]); return $client->token->sendToken('2347041945964', '{token} is your friendship verification token', [ "attempts" => 10, "time_to_live" => 30, "length" => 6, "placeholder" => '{token}', 'type' => 'NUMERIC', ], 'Olawale', 'generic'); // OR probably omit sender id or channel return $client->token->sendToken('2347041945964', '{token} is your friendship verification token', [ "attempts" => 10, "time_to_live" => 30, "length" => 6, "placeholder" => '{token}', 'type' => 'NUMERIC', ], null, 'generic');
Verify Token
Uses Verify Token.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); return $client->token->verify('a2d671d7-e4fd-41d5-9b13-30c192309306', '123456');
For men and women of few words
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); // Returns True if token is verified else returns false return $client->token->verified('a2d671d7-e4fd-41d5-9b13-30c192309306', '123456'); // Returns True if token fails to verify else returns false return $client->token->failed('a2d671d7-e4fd-41d5-9b13-30c192309306', '123456'); // Returns True if token exists but has expired else returns false return $client->token->expired('a2d671d7-e4fd-41d5-9b13-30c192309306', '123456');
Note The above example will make you send multiple requests. Use the below helpers instead.
$client = new Client('{api_key}'); $response = $client->token->verify('a2d671d7-e4fd-41d5-9b13-30c192309306', '123456'); // Check for verified if ($response->verified()) { // Do something } // Check for failed response if ($response->failed()) { // Do something } // Check for expired response if ($response->expired()) { // Do something } // Check for not found response if ($response->notFound()) { // Do something }
Custom error handlers
$client = new Client('{api_key}'); $response = $client->token->verify('a2d671d7-e4fd-41d5-9b13-30c192309306', '123456'); // Run if verification was successful $response->onVerified(function ($response) { // Do something }); // Run if verification failed $response->onFailed(function ($response) { // Do something }); // Run if verification token expired $response->onExpired(function ($response) { // Do something });
Send In App Token
Uses Send In App Token.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); return $client->token->sendInAppToken('2347041945964', [ "attempts" => 10, "time_to_live" => 30, "length" => 6, ]);
Account insights
Balance
Uses Balance.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); return $client->insights->balance();
Inbox
Uses Inbox.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); return $client->insights->inbox(); /** * Get only the data of a specific message by passing its message_id */ return $client->insights->inbox('43224343447041945964');
Search
Uses Search.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); return $client->insights->search('2347041945964');
The search Api is used majorly for checking if a number is DND active, so there are two helper functions to ease the check
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); return $client->insights->isDnd('2347041945964'); // OR return $client->insights->isNotDnd('2347041945964');
Note The above example will make you send multiple requests. Use the below helpers instead.
$client = new Client('{api_key}'); $response = $client->insights->search('2347041945964');; // Check if the number is dnd active or not if ($response->dnd()) { // Do something }
Status
Uses Status.
<?php // This file is generated by Composer require_once __DIR__ . '/vendor/autoload.php'; use ManeOlawale\Termii\Client; $client = new Client('{api_key}'); return $client->insights->number('2347041945964');
Http Manager
In some situations where you want to be responsible for handling the http requests that termii Client sends out, write your own http manager. if you do not use any http manager the client will use the bult-in guzzle manager.
<?php namespace ManeOlawale\Termii\HttpClient; use Psr\Http\Message\ResponseInterface; interface HttpManagerInterface { /** * Handle GET method * * @since 1.2 * * @param string $method * @param string $route * @param array $data * @return \Psr\Http\Message\ResponseInterface */ public function request(string $method, string $route, array $data): ResponseInterface; } class GuzzleHttpManager implements HttpManagerInterface { // }
You can set the Http Manager directly by passing it as the third argument of the client contructor
<?php use ManeOlawale\Termii\HttpClient\GuzzleHttpManager; $client = new Client('{api_key}', [/* Config */], new GuzzleHttpManager());
Set http manager using the setter method:
<?php use ManeOlawale\Termii\HttpClient\GuzzleHttpManager; $client = new Client('{api_key}', [/* Config */]); $client->setHttpManager(new GuzzleHttpManager());
mane-olawale/termii 适用场景与选型建议
mane-olawale/termii 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.29k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mane-olawale/termii 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mane-olawale/termii 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 18.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-15