如何在 Amazon SNS 下订阅多个电话号码到一个主题?
Amazon 提供了下面的 php 示例代码来订阅某个主题的号码。然而,这一次只能添加一个个数字。
如何将多个号码($endpoint)添加到同一主题?最后需要什么代码?
PHP 示例代码:
<?php
// snippet-start:[sns.php.subscribe_text_sms.complete]
// snippet-start:[sns.php.subscribe_text_sms.import]
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
// snippet-end:[sns.php.subscribe_text_sms.import]
/**
* Prepares to subscribe an endpoint by sending the endpoint a confirmation message.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
// snippet-start:[sns.php.subscribe_text_sms.main]
$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());
}
// snippet-end:[sns.php.subscribe_text_sms.main]
// snippet-end:[sns.php.subscribe_text_sms.complete]
// snippet-sourcedescription:[SubscribeTextSMS.php demonstrates how to send a confirmation message as a text message.]
Amazon provides the php example code below to subscribe a number to a topic. However, that only adds one number at a time.
How can I add multiple numbers ($endpoint) to the same topic? What would the code need to be at the end?
PHP Example Code:
<?php
// snippet-start:[sns.php.subscribe_text_sms.complete]
// snippet-start:[sns.php.subscribe_text_sms.import]
require 'vendor/autoload.php';
use Aws\Sns\SnsClient;
use Aws\Exception\AwsException;
// snippet-end:[sns.php.subscribe_text_sms.import]
/**
* Prepares to subscribe an endpoint by sending the endpoint a confirmation message.
*
* This code expects that you have AWS credentials set up per:
* https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials.html
*/
// snippet-start:[sns.php.subscribe_text_sms.main]
$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());
}
// snippet-end:[sns.php.subscribe_text_sms.main]
// snippet-end:[sns.php.subscribe_text_sms.complete]
// snippet-sourcedescription:[SubscribeTextSMS.php demonstrates how to send a confirmation message as a text message.]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案实际上非常简单。我只需要添加一个数字列表(通过数组)并创建一个 foreach 循环:
The solution was actually pretty simple. I just needed to add a list of numbers (through an array) and create a foreach loop: