如何在 Amazon SNS 下订阅多个电话号码到一个主题?

发布于 2025-01-15 01:10:11 字数 1507 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彻夜缠绵 2025-01-22 01:10:11

解决方案实际上非常简单。我只需要添加一个数字列表(通过数组)并创建一个 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.]

The solution was actually pretty simple. I just needed to add a list of numbers (through an array) and create a foreach loop:

<?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.]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文