private function subscribePlatform($platform, $topic) { foreach ($this->sns->getPaginator('ListEndpointsByPlatformApplication', ['PlatformApplicationArn' => $this->arns[$platform]]) as $endpoint) { $this->logger && $this->logger->info('Subscribing device to topic', ['device' => $endpoint['EndpointArn'], 'topic' => $topic, 'platform' => $platform]); try { $this->sns->subscribe(['TopicArn' => $topic, 'Protocol' => 'application', 'Endpoint' => $endpoint['EndpointArn']]); } catch (SnsException $e) { $this->logger && $this->logger->info('Error subscribing device to topic', ['device' => $endpoint['EndpointArn'], 'topic' => $topic, 'platform' => $platform, 'exception' => $e]); } } }
protected function execute(InputInterface $input, OutputInterface $output) { $noWatch = $input->getOption('no-watch'); $queueId = $this->getQueueId(); $snsConfig = $this->getContainer()->getParameter('sns'); $region = isset($snsConfig['region']) ? $snsConfig['region'] : 'us-east-1'; /** @var QueueFactory $queueFactory */ $queueFactory = $this->getContainer()->get('syrup.queue_factory'); $sqs = $queueFactory->create($queueId, $region, $snsConfig['key'], $snsConfig['secret']); /** @var Connection $conn */ $conn = $this->getContainer()->get('doctrine.dbal.syrup_connection'); $stmt = $conn->query("SELECT * FROM queues WHERE id='{$queueId}'"); $res = $stmt->fetchAll(); if (empty($res)) { $conn->insert('queues', ['id' => $queueId, 'access_key' => $snsConfig['key'], 'secret_key' => $snsConfig['secret'], 'region' => $region, 'url' => $sqs->get('QueueUrl')]); } $sqsClient = new SqsClient(['region' => $region, 'version' => '2012-11-05', 'credentials' => ['key' => $snsConfig['key'], 'secret' => $snsConfig['secret']]]); $sqsArn = $sqsClient->getQueueArn($sqs->get('QueueUrl')); // subscribe SQS to SNS $snsClient = new SnsClient(['region' => $region, 'version' => '2010-03-31', 'credentials' => ['key' => $snsConfig['key'], 'secret' => $snsConfig['secret']]]); $snsClient->subscribe(['TopicArn' => $snsConfig['topic_arn'], 'Protocol' => 'sqs', 'Endpoint' => $sqsArn]); // add policy to SQS to allow SNS sending messages to it $sqsPolicy = '{ "Version": "2008-10-17", "Id": "' . $sqsArn . '/SQSDefaultPolicy", "Statement": [ { "Sid": "sqs-sns", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": "SQS:SendMessage", "Resource": "' . $sqsArn . '", "Condition": { "ArnEquals": { "aws:SourceArn": "' . $snsConfig['topic_arn'] . '" } } } ] }'; $sqsClient->setQueueAttributes(['QueueUrl' => $sqs->get('QueueUrl'), 'Attributes' => ['Policy' => $sqsPolicy]]); $output->writeln("SQS created and registered to SNS"); // Add Cloudwatch alarm if (!$noWatch) { $cwClient = new CloudWatchClient(['region' => $region, 'version' => '2010-08-01', 'credentials' => ['key' => $snsConfig['key'], 'secret' => $snsConfig['secret']]]); $cwClient->putMetricAlarm(['AlarmName' => sprintf('Syrup %s queue is full', $queueId), 'ActionsEnabled' => true, 'AlarmActions' => [$snsConfig['alarm_topic_arn']], 'MetricName' => 'ApproximateNumberOfMessagesVisible', 'Namespace' => 'AWS/SQS', 'Statistic' => 'Average', 'Dimensions' => [['Name' => 'QueueName', 'Value' => $queueId]], 'Period' => 300, 'EvaluationPeriods' => 1, 'Threshold' => 5, 'ComparisonOperator' => 'GreaterThanOrEqualToThreshold']); $output->writeln("Cloudwatch alarm created"); } }
/** * Subscribes an endpoint to a SNS Topic * * @param string $topicArn The ARN of the Topic * @param string $protocol The protocol of the Endpoint * @param string $endpoint The Endpoint of the Subscriber * * @return string */ public function subscribeToTopic($topicArn, $protocol, $endpoint) { // Check against the current Topic Subscriptions $subscriptions = $this->getTopicSubscriptions($topicArn); foreach ($subscriptions as $subscription) { if ($endpoint === $subscription['Endpoint']) { return $subscription['SubscriptionArn']; } } $result = $this->sns->subscribe(['TopicArn' => $topicArn, 'Protocol' => $protocol, 'Endpoint' => $endpoint]); $arn = $result->get('SubscriptionArn'); $context = ['Endpoint' => $endpoint, 'Protocol' => $protocol, 'SubscriptionArn' => $arn]; $this->log(200, "Endpoint Subscribed to SNS Topic", $context); return $arn; }
/** * @depends testCreatesTopic */ public function testSubscribesToTopic($topicArn) { // Create an SQS queue for the test self::log('Creating a SQS queue'); $result = $this->sqs->createQueue(array('QueueName' => self::$queueName)); self::$queueUrl = $result['QueueUrl']; $queueArn = $this->sqs->getQueueArn(self::$queueUrl); // Subscribe to the SNS topic using an SQS queue self::log('Subscribing to the topic using the queue'); $result = $this->sns->subscribe(array('TopicArn' => self::$topicArn, 'Endpoint' => $queueArn, 'Protocol' => 'sqs')); // Ensure that the result has a SubscriptionArn self::log('Subscribe result: ' . var_export($result->toArray(), true)); $this->assertArrayHasKey('SubscriptionArn', $result->toArray()); self::$subscriptionArn = $result['SubscriptionArn']; return self::$subscriptionArn; }
$phone = $_POST['phone']; $s3rawurl = $url; $filename = basename($_FILES['userfile']['name']); $s3finishedurl = $finishedurl; $status = 0; $date = '2015-11-10 12:00:00'; $stmt->bind_param("sssssii", $email, $phone, $filename, $s3rawurl, $s3finishedurl, $state, $date); if (!$stmt->execute()) { echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; } printf("%d Row inserted.\n", $stmt->affected_rows); /* explicit close recommended */ $stmt->close(); $link->real_query("SELECT * FROM arshadsTable"); $res = $link->use_result(); echo "Result set order...\n"; while ($row = $res->fetch_assoc()) { echo $row['ID'] . " " . $row['email'] . " " . $row['phone']; } $sns = new Aws\Sns\SnsClient(['version' => 'latest', 'region' => 'us-east-1']); $result = $sns->createTopic(['Name' => 'My-New-SNS-topic']); $topicArn = $result['TopicArn']; echo "Topic ARN is ::: {$topicArn}"; $result = $sns->setTopicAttributes(['AttributeName' => 'DisplayName', 'AttributeValue' => 'MP2-SNS-TOPIC', 'TopicArn' => $topicArn]); $result = $sns->subscribe(['Endpoint' => $email, 'Protocol' => 'email', 'TopicArn' => $topicArn]); sleep(30); //=======================sleep for 30 seconds so that the user can subscribe================== $result = $sns->publish(['TopicArn' => $topicArn, 'Subject' => 'Image uploaded', 'Message' => 'Congratulations! Your image has been successfully uploaded']); $link->close(); header('Location: gallery.php'); exit;
<?php session_start(); require 'vendor/autoload.php'; use Aws\Sns\SnsClient; //Create topic and subscribe to it $email = $_POST['email']; $sns = new Aws\Sns\SnsClient(array('version' => 'latest', 'region' => 'us-east-1')); $ArnArray = $sns->createTopic(['Name' => 'mp2-jgl-pict']); $Arn = $ArnArray['TopicArn']; $settopicAttributes = $sns->setTopicAttributes(array('TopicArn' => "{$Arn}", 'AttributeName' => 'DisplayName', 'AttributeValue' => 'mp2-jgl-pict')); $topicAttributes = $sns->getTopicAttributes(array('TopicArn' => "{$Arn}", 'AttributeName' => 'DisplayName', 'AttributeValue' => 'mp2-jgl-pict')); $listSubscriptions = $sns->listSubscriptionsByTopic(array('TopicArn' => $Arn)); $subscribe = $sns->subscribe(array('TopicArn' => $Arn, 'Protocol' => 'email', 'Endpoint' => $email)); ?> <html> <body style="background-color:LemonChiffon"> <center> <h1 style=> Subscription confirmed</h1> <h2> <?php echo $email; ?> </h2> <h3> <?php echo "\r\n"; echo "You will receive an email you must confirm"; ?> </h3><a href="/index.php">Index page</a></center>
/** * Subscribe a device to the topic, will create new numbered topics * once the first is full * * @param string $deviceArn * @param string $topicArn The base name of the topics to use * @deprecated use SnsClient directly to subscribe * @see SnsClient::subscribe */ public function registerDeviceOnTopic($deviceArn, $topicArn) { $this->sns->subscribe(['TopicArn' => $topicArn, 'Protocol' => 'application', 'Endpoint' => $deviceArn]); }
/** * @Given /^I subscribe endpoint "([^"]*)" with protocol "([^"]*)" to topic "([^"]*)"$/ */ public function iSubscribeEndpointWithProtocolToTopic($endpoint, $protocol, $name) { $this->sns->subscribe(['Endpoint' => $endpoint, 'Protocol' => $protocol, 'TopicArn' => $this->getArnByName($name)]); }