chrisbarr / amazon-sns-php-api
Amazon SNS PHP API
1.8.0
2018-04-04 15:49 UTC
Requires
- php: >=5.2
This package is auto-updated.
Last update: 2024-09-29 04:03:20 UTC
README
此API包装器是官方 Amazon aws-sdk-for-php 的轻量级替代品,用于使用PHP访问Amazon SNS(简单通知服务)
有关Amazon SNS的更多信息,请在此处查看 - http://aws.amazon.com/sns
要使用此包装器,您必须使用PHP5和cURL,并且有一个 Amazon AWS账户
基本使用
使用命令行中的 Composer 安装
$ composer require chrisbarr/amazon-sns-php-api
或将其添加到您的composer.json文件中
{
...
"require": {
"chrisbarr/amazon-sns-php-api": "~1.0"
}
}
示例用法
<?php require 'vendor/autoload.php'; // Create an instance $AmazonSNS = new AmazonSNS(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_ACCESS_KEY); // Create a Topic $topicArn = $AmazonSNS->createTopic('My New SNS Topic'); // Set the Topic's Display Name (required) $AmazonSNS->setTopicAttributes($topicArn, 'DisplayName', 'My SNS Topic Display Name'); // Subscribe to this topic $AmazonSNS->subscribe($topicArn, 'email', 'example@github.com'); // And send a message to subscribers of this topic $AmazonSNS->publish($topicArn, 'Hello, world!');
API方法
可用方法
addPermission($topicArn, $label, $permissions)confirmSubscription($topicArn, $token)createTopic($name)deleteTopic($topicArn)getTopicAttributes($topicArn)listSubscriptions()listSubscriptionsByTopic($topicArn)listTopics()publish($topicArn, $message, $subject, $messageStructure)removePermission($topicArn, $label)setTopicAttributes($topicArn, $attrName, $attrValue)subscribe($topicArn, $protocol, $endpoint)unsubscribe($subscriptionArn)createPlatformEndpoint($platformApplicationArn, $token, $userData)deleteEndpoint($deviceArn)publishToEndpoint($deviceArn,$message)
设置API区域(us-east-1、us-west-2、us-west-1、eu-west-1等)
setRegion($region)
默认API区域为us-east-1
进一步示例
确保在必要时捕获异常
<?php require 'vendor/autoload.php'; $AmazonSNS = new AmazonSNS(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_ACCESS_KEY); $AmazonSNS->setRegion('eu-west-1'); try { $topics = $AmazonSNS->listTopics(); } catch(SNSException $e) { // Amazon SNS returned an error echo 'SNS returned the error "' . $e->getMessage() . '" and code ' . $e->getCode(); } catch(APIException $e) { // Problem with the API echo 'There was an unknown problem with the API, returned code ' . $e->getCode(); }