我在使用Amazon AWS SNS时遇到一个奇怪的问题:使用aws-php-sdk
创建订阅和主题时,总是存在“幽灵”或“不可见”订阅。
如您所见,此订阅存在于“订阅”标签中。但是,当我单击主题链接(此处为cav_56826
)时,我看不到任何订阅。
你们已经遇到过类似的问题吗?这怎么可能发生?
这是我的简化代码:
try
{
$arn = "arn:aws:sns:eu-west-1:XXXXXXXXXXXXXXXXX:app/APNS_VOIP_SANDBOX/ios_cav";
$topics = array("allUsers", "cav_56826");
$topicsToSubcribe = array();
foreach ($topics as $topic)
{
$res = $this->snsClient->createTopic(['Name' => $topic]);
if ($res->get('@metadata')['statusCode'] == 200)
{
array_push($topicsToSubcribe, $res->get('TopicArn'));
}
else
{
throw new Exception("An error occured during Amazon SNS createTopic", $res->get('@metadata')['statusCode']);
}
}
$SNSEndPointData = $this->snsClient->createPlatformEndpoint([
'PlatformApplicationArn' => $arn,
'Token' => $token
]);
foreach ($topicsToSubcribe as $topic)
{
$this->snsClient->subscribe([
'Protocol' => "application",
'Endpoint' => $SNSEndPointData->get('EndpointArn'),
'TopicArn' => $topic
]);
}
}
catch (\Exception $e)
{
// Logs some errors
}
答案 0 :(得分:2)
在进行了一些更改之后,使用PHP 5.6.40
和AWS开发工具包PHP 3.122.0
(已找到here)和下面的代码,我可以看到预期的/正确的行为正在发生。
<?php
require '/usr/src/myapp/aws.phar';
$sdk = new Aws\Sdk([
'region' => 'us-east-1',
'version' => 'latest',
]);
$snsClient = $sdk->createSns();
$token = "XX:YY";
try
{
$arn = "arn:aws:sns:us-east-1:360479286475:app/GCM/test-stackoverflow";
$topics = array("allUsers", "cav_56826");
$topicsToSubcribe = array();
foreach ($topics as $topic)
{
$res = $snsClient->createTopic(['Name' => $topic]);
if ($res->get('@metadata')['statusCode'] == 200)
{
array_push($topicsToSubcribe, $res->get('TopicArn'));
}
else
{
throw new Exception("An error occured during Amazon SNS createTopic", $res->get('@metadata')['statusCode']);
}
}
$SNSEndPointData = $snsClient->createPlatformEndpoint([
'PlatformApplicationArn' => $arn,
'Token' => $token
]);
foreach ($topicsToSubcribe as $topic)
{
$snsClient->subscribe([
'Protocol' => "application",
'Endpoint' => $SNSEndPointData->get('EndpointArn'),
'TopicArn' => $topic
]);
}
}
catch (\Exception $e)
{
// Logs some errors
}
?>
希望这会有所帮助。