使用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 Notification Service(AmaAmazon Simple Queue Service (Amazon SQS) imple Notification ServiceAmazon SMS)、Amazon Server Migration Service () 或Amazon Lambda

订阅将附加到某个主题,该主题管理将消息发送给订阅者。在Amazon SDK for PHP版本 3 的 Amazon SNS 中管理主题中了解有关创建主题的更多信息。

以下示例演示如何:

有关使用 Amazon SNS 的更多信息,请参阅使用 Amazon SNS 发送系统间消息传送的更多信息,请参阅使用 Amazon SNS 发送系统

的所有示例代码都可以在此Amazon SDK for PHP处找到 GitHub

凭证

在运行示例代码之前,请配置您的Amazon证书,如中所述设置凭证。然后导入Amazon SDK for PHP,如中所述基本用法

订阅主题的电子邮件地址

要建立电子邮件地址订阅,请使用 Subscribe 操作。

您可以使用订阅方法为多个不同的终端节点订阅 Amazon SNS 主题,具体取决于传递的参数所使用的值。本主题中的其他示例演示了这一点。

在本示例中,终端节点是电子邮件地址。将向该电子邮件发送确认令牌。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。

导入

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' ]); $protocol = 'email'; $endpoint = 'sample@example.com'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->subscribe([ 'Protocol' => $protocol, 'Endpoint' => $endpoint, 'ReturnSubscriptionArn' => true, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

为应用程序终端节点的Name Notification

要建立 Web 应用程序订阅,请使用 Subscribe 操作。

您可以使用订阅方法为多个不同的终端节点订阅 Amazon SNS 主题,具体取决于传递的参数所使用的值。本主题中的其他示例演示了这一点。

在此示例中,终端节点是 URL。将会向此 Web 地址发送确认令牌。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。

导入

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' ]); $protocol = 'https'; $endpoint = 'https://'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->subscribe([ 'Protocol' => $protocol, 'Endpoint' => $endpoint, 'ReturnSubscriptionArn' => true, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

向主题订阅 Lambda 函数

要启动对 Lambda 函数的订阅,请使用订阅操作。

您可以使用订阅方法为多个不同的终端节点订阅 Amazon SNS 主题,具体取决于传递的参数所使用的值。本主题中的其他示例演示了这一点。

在本示例中,终端节点是 Lambda 函数。确认令牌已发送到此 Lambda 函数。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。

导入

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' ]); $protocol = 'lambda'; $endpoint = 'arn:aws:lambda:us-east-1:123456789023:function:messageStore'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->subscribe([ 'Protocol' => $protocol, 'Endpoint' => $endpoint, 'ReturnSubscriptionArn' => true, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

向主题订阅 SNS NS 主题

要同时将 SMS 消息发送到多个电话号码,请将各个号码订阅到主题。

要建立电话号码订阅,请使用 Subscribe 操作。

您可以使用订阅方法为多个不同的终端节点订阅 Amazon SNS 主题,具体取决于传递的参数所使用的值。本主题中的其他示例演示了这一点。

在此示例中,终端节点是 E.164 格式的电话号码,这是国际电信的标准。

将会向此电话号码发送确认令牌。在收到电子邮件的 3 天内,使用此确认令牌验证订阅。

有关使用 Amazon SNS 发送 SMS 消息的另一种方式,请参阅使用Amazon SDK for PHP版本 3 在 Amazon SNS 中发送 SMS 消息

导入

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' ]); $protocol = 'sms'; $endpoint = '+1XXX5550100'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->subscribe([ 'Protocol' => $protocol, 'Endpoint' => $endpoint, 'ReturnSubscriptionArn' => true, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

确认订阅主题

要实际创建订阅,终端节点所有者必须使用在最初建立订阅时发送的令牌,确认接收来自该主题的消息的意图,如前所述。确认令牌有效期为 3 天。3 天之后,您可以通过创建新订阅来重新发送令牌。

要确认订阅,请使用ConfirmSubscription操作。

导入

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' ]); $subscription_token = 'arn:aws:sns:us-east-1:111122223333:MyTopic:123456-abcd-12ab-1234-12ba3dc1234a'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->confirmSubscription([ 'Token' => $subscription_token, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

列出主题的订阅列表

要在给定Amazon区域中列出最多 100 个现有订阅,请使用ListSubscriptions操作。

导入

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->listSubscriptions([ ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

取消订阅主题

要删除订阅到某个主题的终端节点,请使用 Unsubscribe 操作。

如果订阅需要身份验证才能删除,则只有订阅的所有者或主题的所有者可以取消订阅,并且需要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' ]); $subscription = 'arn:aws:sns:us-east-1:111122223333:MySubscription'; try { $result = $SnSclient->unsubscribe([ 'SubscriptionArn' => $subscription, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }

将消息发布到 Amazon SNS 主题

要向订阅了 Amazon SNS 主题的每个终端节点传送消息,请使用发布操作。

创建一个包含发布消息的参数的对象,包括消息文本和 Amazon SNS 主题的 Amazon SNS 主题的 Amazon SNS 主题的 Amazon SNS 主题ARN Amazon SNS 主题的 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' ]); $message = 'This message is sent from a Amazon SNS code sample.'; $topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic'; try { $result = $SnSclient->publish([ 'Message' => $message, 'TopicArn' => $topic, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails error_log($e->getMessage()); }