本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用Amazon SDK for PHP版本 3 在 Amazon SNS 中管理主题
要向Amazon Simple Queue Service (Amazon SQS)Amazon SMS、HTTP/HTTPS URLAmazon Lambda、电子邮件或发送通知,您必须首先创建一个主题来管理向该主题的任何订阅者发送消息。
对于观察程序设计模式,此主题 (Topic) 类似于该主题 (Subject)。创建主题后,您可以添加在有消息发布到该主题时将自动通知的订阅者。
在Amazon SDK for PHP版本 3 的 Amazon SNS 中管理订阅中了解有关订阅主题的更多信息。
以下示例演示如何:
-
创建要向其发布通知的主题CreateTopic。
-
使用返回请求者的主题列表ListTopics。
-
使用删除主题及其所有订阅DeleteTopic。
-
使用返回主题的所有属性GetTopicAttributes。
-
允许主题所有者使用将主题的属性设置为新值SetTopicAttributes。
有关使用 Amazon SNS 主题属性。有关使用 Amazon SNS 主题属性。
的所有示例代码都可以在此Amazon SDK for PHP处找到 GitHub
凭证
在运行示例代码之前,请按照中的说明配置您的Amazon证书设置凭证。然后导入Amazon SDK for PHP,如中所述基本用法。
创建主题
要创建主题,请使用CreateTopic操作。
您Amazon账户中的每个主题名称都必须是唯一的。
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topicname = 'myTopic'; try { $result = $SnSclient->createTopic([ 'Name' => $topicname, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
列出主题
要列出当前Amazon区域中最多 100 个现有主题,请使用ListTopics操作。
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); try { $result = $SnSclient->listTopics([ ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
删除主题
要删除现有主题及其所有订阅,请使用DeleteTopic操作。
尚未传送至订阅者的任何消息也将删除。
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->deleteTopic([ 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
获取主题属性
要检索单个现有主题的属性,请使用GetTopicAttributes操作。
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->getTopicAttributes([ 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }
设置主题属性
要更新单个现有主题的属性,请使用SetTopicAttributes操作。
您只能设置 Policy
、DisplayName
和 DeliveryPolicy
属性。
导入
require 'vendor/autoload.php'; use Aws\Sns\SnsClient; use Aws\Exception\AwsException;
示例代码
$SnSclient = new SnsClient([ 'profile' => 'default', 'region' => 'us-east-1', 'version' => '2010-03-31' ]); $attribute = 'Policy | DisplayName | DeliveryPolicy'; $value = 'First Topic'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->setTopicAttributes([ 'AttributeName' => $attribute, 'AttributeValue' => $value, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }