uwd / smsglobal-rest-api-v2
SMSGlobal的REST API v2的包装器
dev-master
2018-02-27 05:25 UTC
Requires
- php: >=5.3.0
Requires (Dev)
Replaces
This package is not auto-updated.
Last update: 2024-09-19 05:13:29 UTC
README
PHP SMSGlobal类库
这是一个SMSGlobal REST API的包装器。您可以通过在MXT平台中注册并查看API密钥页面来从SMSGlobal获取API密钥。
查看REST API文档,了解可用资源列表。
快速入门
此包装器需要PHP 5.3或更高版本,以及安装和启用的cURL库或HTTP流包装器。
要安装,请将依赖项添加到您的composer.json
文件中
{ "require": { "uwd/smsglobal-rest-api-v2": "*" } }
然后使用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\RestApiClient2\ApiKey; use Smsglobal\RestApiClient2\Resource\Sms; use Smsglobal\RestApiClient2\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));
注意事项
- 在单个会话中两次请求相同的对象将返回相同的实例(即使在资源列表中)
- 如果您尝试保存包含无效数据的对象,将抛出异常