smsglobal/rest-api-client

此包已被 弃用 并不再维护。作者建议使用 https://github.com/smsglobal/smsglobal-php 包代替。

SMSGlobal REST API 封装器

dev-master 2018-02-02 04:15 UTC

This package is auto-updated.

Last update: 2020-09-17 09:10:58 UTC


README

SMSGlobal PHP 类库

这是一个 SMSGlobal REST API 的封装器。通过在 MXT 平台注册并查看 API 密钥页面,从 SMSGlobal 获取 API 密钥。

查看 REST API 文档 获取可用资源的列表。

快速开始

此封装器需要 PHP 5.3 或更高版本,以及安装并启用 cURL 库或 HTTP 流封装器。

要安装,请在 composer.json 文件中添加依赖项

{
    "require": {
        "smsglobal/rest-api-client": "*"
    }
}

然后使用 Composer 进行安装。

$ cd path/to/your/project
$ composer install --no-dev

不使用 Composer?

您可以从 GitHub 下载库。只需使用 PSR-0 合规的自动加载器加载类。

要运行单元测试或生成文档,您需要 PHPUnit 和 phpDocumentor。

运行单元测试

$ cd path/to/SMSGlobal/rest/api/client
$ composer install
$ phpunit

获取文档

文档可在 SMSGlobal 网站上找到,或您可以自己生成它

$ cd path/to/SMSGlobal/rest/api/client
$ composer install
$ vendor/phpdocumentor/phpdocumentor/bin/phpdoc.php -c phpdoc.xml

使用库

运行单元测试

// Include the Composer autoloader or use your own PSR-0 autoloader
require 'vendor/autoload.php';

use Smsglobal\RestApiClient\ApiKey;
use Smsglobal\RestApiClient\Resource\Sms;
use Smsglobal\RestApiClient\RestApiClient;

// Get an API key from SMSGlobal and insert the key and secret
$apiKey = new ApiKey('your-api-key', 'your-api-secret');

// All requests are done via a 'REST API client.' This abstracts away the REST
// API so you can deal with it like you would an ORM
$rest = new RestApiClient($apiKey);

// Now you can get objects
$contact = $rest->get('contact', 1); // Contact resource with ID = 1
// Edit them
$contact->setMsisdn('61447100250');
// And save them
$rest->save($contact);
// Or delete them
$rest->delete($contact);

// You can also instantiate new resources
$sms = new Sms();
$sms->setDestination('61447100250')
    ->setOrigin('Test')
    ->setMessage('Hello World');
// And save them
$rest->save($sms);
// When a new object is saved, the ID gets populated (it was null before)
echo $sms->getId(); // integer

// For an SMS, saving also sends the message, so you can use a more meaningful
// keyword for it
$sms->send($rest);

// You can get a list of available resources
$list = $rest->getList('sms');

foreach ($list->objects as $resource) {
    // ...
}

// Pagination data is included
echo $list->meta->getTotalPages(); // integer

// Lists can be filtered
// e.g. contacts belonging to group ID 1
$rest->getList('contact', 0, 20, array('group' => 1));

注意

  1. 在会话中两次请求相同的对象将返回相同的实例(即使在资源列表中)
  2. 如果您尝试保存无效数据的对象,将抛出异常