Amazon SNS下如何订阅多个phone个号到一个主题?
How to subscribe multiple phone numbers to a topic under Amazon SNS?
亚马逊提供了下面的 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.]
解决方案实际上非常简单。我只需要添加一个数字列表(通过数组)并创建一个 foreach 循环:
<?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';
$endpoints = array('+1XXX5550100', '+2XXX5550100', '+3XXX5550100');
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
foreach ($endpoints as $endpoint) {
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.]
亚马逊提供了下面的 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.]
解决方案实际上非常简单。我只需要添加一个数字列表(通过数组)并创建一个 foreach 循环:
<?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';
$endpoints = array('+1XXX5550100', '+2XXX5550100', '+3XXX5550100');
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
foreach ($endpoints as $endpoint) {
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.]