本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在使用 Amazon SDK for PHP 版本 3 的 Amazon SNS 中管理主题
要将通知发送到 Amazon Simple Queue Service (Amazon SQS)、HTTP/HTTPS URL、电子邮件、Amazon SMS 或 Amazon 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 Web Services 账户 中的各个主题名称必须唯一。
导入
require 'vendor/autoload.php'; use Aws\Exception\AwsException; use Aws\Sns\SnsClient;
示例代码
$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\Exception\AwsException; use Aws\Sns\SnsClient;
示例代码
$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\Exception\AwsException; use Aws\Sns\SnsClient;
示例代码
$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\Exception\AwsException; use Aws\Sns\SnsClient;
示例代码
$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\Exception\AwsException; use Aws\Sns\SnsClient;
示例代码
$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()); }