isurindu / rest-api-client
此包已被废弃且不再维护。未建议替代包。
用于 SMSGlobal 的 REST API v2 的包装器
2.4
2018-02-27 01:35 UTC
Requires
- php: >=5.3.0
Requires (Dev)
Replaces
This package is not auto-updated.
Last update: 2022-02-01 13:12:24 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));
注意
- 在一个会话中两次请求相同的对象将返回相同的实例(即使在资源列表中)
- 如果您尝试保存包含无效数据的对象,将抛出异常