inmobile/inmobile-sdk

InMobile API交互的官方PHP库

4.1.0.109 2024-03-14 10:58 UTC

This package is auto-updated.

Last update: 2024-09-14 12:00:56 UTC


README

要求

  • PHP 7.4, 8.0, 8.1, 8.2 或 8.3。

入门

客户端的最新版本可以在这里找到

  1. composer require inmobile/inmobile-sdk
  2. 初始化InmobileApi类并开始发送消息!

每个端点都分割成一个不同的类。

因此,可以通过调用->messages()访问消息API,通过调用->lists()访问列表API,依此类推。

示例

/*
 * Require autoload, to automatically load the SDK, after installing via composer.
 * Execute the following command now, if you haven't already: composer require inmobile/inmobile-sdk
 */
require_once __DIR__ . '/vendor/autoload.php';

use Inmobile\InmobileSDK\InmobileApi;
use Inmobile\InmobileSDK\RequestModels\Message;

/*
 * Initialize the Inmobile API Client
 */
$api = new InmobileApi('my-api-token');

/*
 * Send the message
 */
$response = $api->messages()->send(
    Message::create('This is a message text to be sent')
        ->from('1245')
        ->to('4512345678')
);

$response->toArray();

/**
 * "results": [
 *     {
 *         "numberDetails": {
 *             "countryCode": "45",
 *             "phoneNumber": "12345678",
 *             "rawMsisdn": "45 12 34 56 78",
 *             "isValidMsisdn": true,
 *             "isAnonymized": false
 *         },
 *         "text": "This is a message text to be sent",
 *         "from": "1245",
 *         "smsCount": 1,
 *         "messageId": "INMBL",
 *         "encoding": "gsm7"
 *     }
 * ]
 */

您可以在这里找到完整的API文档

端点

SDK被分割成不同的类,每个类对应InMobile API中的不同“端点”。

消息

可以通过在InmobileApi上调用->messages()来访问。以下是一个所有操作的示例。

发送消息

发送一条或多条消息。

$api->messages()->send(
    Message::create('Hello World')
        ->from('INMBL')
        ->to(4512345678)
);

$api->messages()->send([
    Message::create('Foobar')
        ->from('INMBL')
        ->to(4512345678),
    Message::create('Barbiz')
        ->from('INMBL')
        ->to(4512345678)
        ->countryHint('DK')
]);

使用模板发送消息

$api->messages()->sendUsingTemplate(
    TemplateMessage::create()
        ->to(4512345678)
        ->setPlaceholders([
            '{name}' => 'John',
            '{lastname}' => 'Doe',
        ]),
    'ecdcb257-c1e9-4b44-8a4e-f05822372d82',
);

// Multiple
$placeholders = [
    '{name}' => 'John',
    '{lastname}' => 'Doe',
];

$api->messages()->sendUsingTemplate([
    TemplateMessage::create()->to(4512345678)->setPlaceholders($placeholders),
    TemplateMessage::create()->to(4512345678)->setPlaceholders($placeholders),
], 'ecdcb257-c1e9-4b44-8a4e-f05822372d82');

使用查询发送消息

使用查询参数发送一条消息。

$api->messages()->sendUsingQuery(
    Message::create('Hello World')
        ->from('INMBL')
        ->to(4512345678)
);

获取状态报告

获取所有短信报告

$api->messages()->getStatusReport($limit = 20);

取消消息

通过ID取消一条或多条消息

$api->messages()->cancel('MESSAGEID-1');

// Or multiple messages
$api->messages()->cancel(['MESSAGEID-1', 'MESSAGEID-2']);

列表

可以通过在InmobileApi上调用->lists()来访问。以下是一个所有操作的示例。

获取所有

获取所有列表。这将自动运行每一页,并返回所有列表的数组。

$api->lists()->getAll();

查找

通过ID查找列表

$api->lists()->find($listId);

创建

创建一个新的列表

$api->lists()->create($listName);

更新

使用新名称更新列表

$api->lists()->update($listId, $newName);

删除

通过ID删除列表

$api->lists()->delete($listId);

黑名单

可以通过在InmobileApi上调用->blacklist()来访问。以下是一个所有操作的示例。

获取

获取黑名单中所有条目的分页列表

$api->blacklist()->get($pageLimit = 20);

获取所有

获取黑名单中所有条目。这将自动运行每一页,并返回所有条目的数组。

$api->blacklist()->getAll();

通过ID查找

通过ID查找条目

$api->blacklist()->findEntryById('ENTRYID-1');

通过电话号码查找

通过电话号码查找条目

$api->blacklist()->findEntryByNumber($countryCode = 45, $phoneNumber = 12345678);

创建

在黑名单中创建一个新的条目

$api->blacklist()->createEntry($countryCode = 45, $phoneNumber = 12345678, $comment = null);

通过ID删除

通过ID删除条目

$api->blacklist()->deleteEntryById('ENTRYID-1');

通过电话号码删除

通过电话号码删除条目

$api->blacklist()->deleteEntryByNumber($countryCode = 45, $phoneNumber = 12345678);

接收者

可以通过在InmobileApi上调用->recipients()来访问。以下是一个所有操作的示例。

获取

获取分页响应,包含列表中所有接收者的信息

$api->recipients()->get($listId = 'LIST-1', $limit = 20);

获取所有

获取列表中所有接收者的信息。这将自动运行每一页,并返回所有接收者的数组。

$api->recipients()->getAll($listId = 'LIST-1');

通过ID查找

通过ID查找接收者

$api->recipients()->findById($listId = 'LIST-1', $id = 'RECIPIENT-1');

通过电话号码查找

通过电话号码查找接收者

$api->recipients()->findByPhoneNumber($listId = 'LIST-1', $countryCode = 45, $phoneNumber = 12345678);

创建

在列表中创建接收者

$api->recipients()->create(
    $listId = 'LIST-1',
    Recipient::create(45, 12345678)
        ->addField('firstname', 'John')
        ->addField('lastname', 'Doe')
        ->createdAt(new DateTime('2021-01-02 03:04:05'))
);

更新

在列表中更新接收者

$api->recipients()->update(
    $listId = 'LIST-1',
    $id = 'RECIPIENT-1',
    Recipient::create(45, 12345678)
        ->addField('firstname', 'John')
        ->addField('lastname', 'Doe')
        ->createdAt(new DateTime('2021-01-02 03:04:05'))
);

创建或更新

如果不存在,则在列表中创建或更新接收者。

$api->recipients()->createOrUpdateByPhoneNumber(
    $listId = 'LIST-1',
    $countryCode = 45,
    $phoneNumber = 12345678,
    Recipient::create(45, 12345678)
        ->addField('firstname', 'John')
        ->addField('lastname', 'Doe')     
);

通过ID删除

通过ID删除

$api->recipients()->deleteById($listId = 'LIST-1', $id = 'RECIPIENT-1');

通过电话号码删除

通过电话号码删除

$api->recipients()->deleteByPhoneNumber($listId = 'LIST-1', $countryCode = 45, $phoneNumber = 12345678);

删除列表中的所有接收者

这将删除给定列表中的所有接收者

$api->recipients()->deleteAllFromList($listId = 'LIST-1');

GDPR

创建信息删除请求

$api->gdpr()->createDeletionRequest(NumberInfo::create($countryCode = '45', $phoneNumber = '12345678'));

工具

解析电话号码

$api->tools()->numbersToParse([
    NumberToParse::create($countryHint = 'DK', $rawMsisdn = '12 34 56 78')
    NumberToParse::create($countryHint = '45', $rawMsisdn = '12 34 56 78')
]);

// If you wish to parse a single number, you can do so by passing a single NumberToParse object
$api->tools()->numbersToParse(NumberToParse::create($countryHint = 'DK', $rawMsisdn = '12 34 56 78'));

模板

获取所有

获取所有模板。这会自动遍历每一页,并返回所有模板的数组。

$api->templates()->getAll();

查找

通过ID查找模板

$api->templates()->find($templateId);

电子邮件

发送电子邮件

发送一封带有指定主题和文本/HTML正文的电子邮件给一个或多个收件人。

$api->emails()->send(
    Email::create()
        ->from(EmailRecipient::create('john@example.com', 'John Doe'))
        ->to(EmailRecipient::create('jane@example.com', 'Jane Doe'))
        ->subject('Hello World')
        ->text('Hello World')
        ->html('<h1>Hello World</h1>')
);

使用模板发送电子邮件

使用模板给一个或多个收件人发送电子邮件。

$api->emails()->sendUsingTemplate(
    Email::create()
        ->from(EmailRecipient::create('john@example.com', 'John Doe'))
        ->to(EmailRecipient::create('jane@example.com', 'Jane Doe'))
        ->templateId('ABCDEF12-3456-7890-ABCD-EF1234567890')
);

获取电子邮件事件

获取分页的电子邮件事件列表。

$api->emails()->getEvents($limit = 20);

电子邮件模板

获取所有

获取所有电子邮件模板。这会自动遍历每一页,并返回所有模板的数组。

$api->emailTemplates()->getAll();

查找

通过ID查找电子邮件模板

$api->emailTemplates()->find($templateId);