Termii SMS API 的简单面向对象 PHP 客户端。

v1.2.3 2022-03-29 14:23 UTC

This package is auto-updated.

Last update: 2024-08-30 01:09:20 UTC


README

Github Total Downloads Latest Stable Version License

Termii 客户端

Termii SMS API 的简单面向对象 PHP 客户端。

使用 Termii API

要求

  • PHP >= 7.2
  • Guzzlehttp ~6|~7

安装

通过 Composer

PHP 7.2+

composer require mane-olawale/termii

现在您已将 Termii 客户端安装在 vendor/mane-olawale/termii

并在 vendor/autoload.php 中包含了方便的项目自动加载文件

Termii 客户端的基本使用

<?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}',
        ]);

增强的 http 响应

<?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
}

错误处理

<?php

// Run on success
$response->onSuccess(function ($response) {
    // Do something
});

// Run on error
$response->onError(function ($response) {
    // Do something
});

您仍然可以将响应视为数组

$message_id = $response['message_id'];

您可以直接在列表响应上使用 foreach,例如 historysender list 等。

<?php

foreach ($response as $sender) {
    // Work with each sender data
}

注意 响应还有更多辅助方法。请参阅完整的文档 此处

发送者

获取发送者 ID 列表

使用 发送者 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();

注意 我们没有添加发送者 ID 和渠道,因为它们是可选的,并且可以在稍后通过客户端对象或 SMS API 处理器传递。

请求发送者 ID

使用 请求发送者 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');

短信

发送短信

使用 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');

自定义发送者 ID 或渠道

<?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');

您可以在发送后立即获取消息资源。

    $client = new Client('{api_key}');

    $response = $client->sms->send('2347041945964', 'Testing');

    $message = $response->message();

发送号码

使用 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');

模板

使用 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}');

令牌

发送令牌

使用 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',
    ]);

自定义发送者 ID 或渠道

<?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

<?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');

对于话不多的男女

<?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');

注意 上述示例将使您发送多个请求。请改用以下辅助器。

    $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
    }

自定义错误处理器

    $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

<?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,
    ]);

账户洞察

余额

使用 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

<?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

<?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');

搜索 API 主要用于检查号码是否为 DND 活动,因此有两个辅助函数来简化检查。

<?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');

注意 上述示例将使您发送多个请求。请改用以下辅助器。

    $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

<?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 管理器

在某些情况下,如果您想负责处理 Termii 客户端发送的 http 请求,请编写自己的 http 管理器。如果不使用任何 http 管理器,则客户端将使用内置的 guzzle 管理器。

<?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
    {
        //
    }

您可以通过将Http Manager作为客户端构造函数的第三个参数传递来直接设置Http Manager。

<?php

    use ManeOlawale\Termii\HttpClient\GuzzleHttpManager;

    $client = new Client('{api_key}', [/* Config */], new GuzzleHttpManager());

使用setter方法设置Http Manager。

<?php

    use ManeOlawale\Termii\HttpClient\GuzzleHttpManager;

    $client = new Client('{api_key}', [/* Config */]);
    
    $client->setHttpManager(new GuzzleHttpManager());