在使用 Amazon SDK for PHP 版本 3 的 Amazon SNS 中管理主题 - Amazon SDK for PHP
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

在使用 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 中管理订阅中了解有关订阅主题的更多信息。

以下示例演示如何:

有关使用 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操作。

您只能设置 PolicyDisplayNameDeliveryPolicy 属性。

导入

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()); }