tm4b/tm4b-php

使用TM4B的REST API的PHP客户端

v1.0.4 2021-02-27 15:10 UTC

This package is auto-updated.

Last update: 2024-09-27 22:46:23 UTC


README

Build Status

注册 TM4B账户并访问我们的开发者API,获取更多内容。

TM4B PHP客户端

这是使用TM4B REST API的官方PHP库。此SDK包含用于轻松与TM4B消息API交互的方法。以下是一些入门示例。更多示例请参阅我们的官方文档:https://www.tm4b.com/en/sms-api/

安装

推荐通过Composer安装TM4B PHP。需要tm4b/tm4b-php

$ composer require tm4b/tm4b-php

使用

基本用法

您应在应用程序中使用Composer的自动加载器自动加载依赖项。以下所有示例都假设您已将其包含在文件中

require 'vendor/autoload.php';

使用您的TM4B API密钥初始化您的TM4B客户端

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

快速入门

以下是一个快速示例

POST /api/rest/v1/sms

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

try {
    $response = $msgClient->messages()->send([
        'destination_address' => '+441234567890',
        'source_address'      => 'master',
        'content'             => 'There is a civil war between the Galactic Empire and a Rebel Alliance.'
    ]);
    print_r($response);
} catch (\Tm4b\Exception\HttpClientException $e) {
    print_r($e->getResponseBody());
}

示例

批量短信

让我们向2个收件人发送相同的短信。

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

$response = $msgClient->messages()->send([
    [
        'destination_address' => '+447711961111',
        'source_address'      => 'GeorgeLucas',
        'content'             => 'There is a civil war between the Galactic Empire and a Rebel Alliance.'
    ],
    [
        'destination_address' => '+97150349030',
        'source_address'      => 'GeorgeLucas',
        'content'             => 'There is a civil war between the Galactic Empire and a Rebel Alliance.'
    ]
]);

沙盒

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

$msgClient->setSandbox(true);
$response = $msgClient->messages()->send([
    [
        'destination_address' => '+447711961111',
        'source_address'      => 'GeorgeLucas',
        'content'             => 'There is a civil war between the Galactic Empire and a Rebel Alliance.'
    ]
]);

账户

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);
$response = $msgClient->account()->describe();

通用用法

<?php

require 'vendor/autoload.php';

$msgClient = \Tm4b\Rest\Client::create([
    'apiKey' => 'TM4B_API_KEY'
]);

try {
    $response = $msgClient->get('/account');
    $data = $msgClient->hydrate($response, null);
    print_r($data);
} catch (\Http\Client\Exception $e) {
    print_r($e->getTraceAsString());
}