The following code allows me to send SNS with (queue) SQS but i cant seem to choose the protocol like in amazons management console
* the keys are all random strings
<?php
require_once('sdk-1.5.6.2/sdk.class.php');
$AWS_KEY = "6VVWTU4JDAAKHYB1C3ZN";
$AWS_SECRET_KEY = "GMSCUD8C0QA1QLV9Y3RP2IAKDIZSCHRGKEJSXZ4F";
//create a new SQS queue and grab the queue URL
$sqs = new AmazonSQS(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY ));
$response = $sqs->create_queue('test-topic-queue');
$queue_url = (string) $response->body->CreateQueueResult->QueueUrl;
$queue_arn = 'arn:aws:sqs:us-east-1:ENCQ8gqrAcXv:test-topic-queue';
//create a new SNS topic and grab the topic ARN.
$sns = new AmazonSNS(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY ));
$response = $sns->create_topic('test-topic');
$topic_arn = (string) $response->body->CreateTopicResult->TopicArn;
//Then give the SNS topic the permission to send messages to the SQS queue. ** allow all principals. SNS doesn't send from your account ID -- it has its own account ID that it sends from.
$queue_url = 'https://sqs.us-east-1.amazonaws.com/ENCQ8gqrAcXv/test-topic-queue';
$queue_arn = 'arn:aws:sqs:us-east-1:ENCQ8gqrAcXv:test-topic-queue';
$topic_arn = 'arn:aws:sns:us-east-1:ENCQ8gqrAcXv:test-topic';
$policy = new CFPolicy($sqs, array(
'Version' => '2008-10-17',
'Id' => 'sampleId',
'Statement' => array(
array(
'Resource' => $queue_arn,
'Effect' => 'Allow',
'Sid' => 'rule1',
'Action' => 'sqs:*',
'Condition' => array(
'StringEquals' => array(
'aws:SourceArn' => $topic_arn
)
),
'Principal' => array(
'AWS' => '*'
)
)
)
));
$response = $sqs->set_queue_attributes($queue_url, array(
array('Name' => 'Policy', 'Value' => $policy->get_json())
));
//then subscribe the SQS queue to the SNS topic and grab the subscription ARN.
$queue_arn = 'arn:aws:sqs:us-east-1:ENCQ8gqrAcXv:test-topic-queue';
$topic_arn = 'arn:aws:sns:us-east-1:ENCQ8gqrAcXv:test-topic';
$response = $sns->subscribe($topic_arn, 'sqs', $queue_arn);
// normally here is where you would choose the protocol but this example sends this to SQS
// subscribe ( $topic_arn, $protocol, $endpoint, $opt )
$subscription_arn = (string) $response->body->SubscribeResult->SubscriptionArn;
//view the list of subscriptions to verify.
$topic_arn = 'arn:aws:sns:us-east-1:ENCQ8gqrAcXv:test-topic';
$q = new CFBatchRequest(200);
for ($i = 0; $i < 1000; $i++)
{
$sns->batch($q)->publish($topic_arn, 'Hello world! ' . time());
}
$response = $sns->batch($q)->send();
//receive messages from the queue.
$queue_url = 'https://sqs.us-east-1.amazonaws.com/ENCQ8gqrAcXv/test-topic-queue';
$response = $sqs->receive_message($queue_url, array(
'MaxNumberOfMessages' => 10,
));
print_r($response);
// delete SQS queue
$queue_url = 'https://sqs.us-east-1.amazonaws.com/ENCQ8gqrAcXv/test-topic-queue';
$response = $sqs->delete_queue($queue_url);
?>
question: where would i choose the protocol?
this link says that subscribe() is where protocol is defined but the above example sends that to SQS
You can use SNS to push to an SMS number, and you can also use SNS to push to an SQS queue. But SQS is not capable of pushing anything — including to an SMS number.
Perhaps you have your infrastructure components confused?
Subscribe method has the following signature
subscribe ( $topic_arn, $protocol, $endpoint, $opt )
If you what to subscribe using SMS as protocol then do the following
$response = $sns->subscribe($topic_arn, 'sms', $phone_no);
where $phone_no(String) is the phone number of the sms enabled device
The different protocols currently supported are http, https, email, email-json, sms and sqs based on the protocol you have to change the $endpoint parameter passed to the subscribe method.
Checkout the full documentation for subscribe method
Related
The following code subscribes to an SNS HTTP Endpoint:
$protocol = 'http';
$endpoint = 'http://test.com/endpoint.php';
$filterPolicyTest = array(
'test' => ['1','2','3']
);
$filterPolicyString = json_encode($filterPolicyTest);
$result = $this->awsClient->subscribe([
'Protocol' => $protocol,
'Endpoint' => $endpoint,
'ReturnSubscriptionArn' => true,
'TopicArn' => 'myTopicARN',
'Attributes' => [
'FilterPolicy' => $filterPolicyString,
],
]);
After confirming the subscription, I can view the subscription in the AWS console and see the Filter policy.
Now I try the same thing but with an email:
$protocol = 'email';
$endpoint = 'myemail#gmail.com';
$filterPolicyTest = array(
'test' => ['1','2','3']
);
$filterPolicyString = json_encode($filterPolicyTest);
$result = $this->awsClient->subscribe([
'Protocol' => $protocol,
'Endpoint' => $endpoint,
'ReturnSubscriptionArn' => true,
'TopicArn' => 'myTopicARN',
'Attributes' => [
'FilterPolicy' => $filterPolicyString,
],
]);
No errors are thrown. I successfully receive a confirmation email and click the link to subscribe. But, in the AWS console for this subscription, I see:
The FilterPolicy attribute is completely ignored.
I am using the PHP SDK version 3. Here is the documentation for this request. I tried submitting a FilterPolicy both as an array and json encoded string with no luck.
I'm using a script to send with Amazon SNS some text messages through their API in PHP. The problem which I am trying to solve is how I can test my script without sending everytime the text message. Is there a possibility to enter a kind of 'developing key' whichs give me a full send report in PHP without sending the text message to my mobile phone? I previous used Messagebird and there is this possible.
Thank you.
require './vendor/autoload.php';
error_reporting(E_ALL);
ini_set("display_errors", 1);
$params = array(
'credentials' => array(
'key' => 'ABCKEY123',
'secret' => 'ABCSECRET123',
),
'region' => 'us-east-1',
'version' => 'latest'
);
$sns = new \Aws\Sns\SnsClient($params);
$args = array(
"SenderID" => "NAME",
"SMSType" => "Transactional",
"Message" => "Message",
"PhoneNumber" => "+31612345678"
);
$result = $sns->publish($args);
echo "<pre>";
var_dump($result);
echo "</pre>";
You would either need to modify your code to NOT send to Amazon SNS, or you could have it send to an Amazon SNS topic that simply sends the message as an email.
I am using PHP to send push notification to browsers using VAPID. I am using web push library to achieve this (https://github.com/web-push-libs/web-push-php) and it is working fine for me.
But issue is sending push notification to 100000+ users is time consuming and also it slow down the server.
How can I send notification to multiple users at a time or send notification to 1000 users at a time using one API call? Any help will be appreciated
require __DIR__ . '/../vendor/autoload.php';
use Minishlink\WebPush\WebPush;
$subscription = json_decode(file_get_contents('php://input'), true);
$auth = array(
'VAPID' => array(
'subject' => 'https://www.example.com',
'publicKey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'privateKey' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
),
);
$webPush = new WebPush($auth);
$res = $webPush->sendNotification(
"https://fcm.googleapis.com/fcm/send/xxxxxxxxxxxxxxxxxxxxxxxxxx",
"hello",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
true
);
I'm creating orders with shopify web app using php, Guzzle 6 hosted on Heroku. Following code snippet is used.
//json encode
$orderdata = array(
'order' => array(
'line_items' => array(
0 => array(
'title' => 'Avocado',
'quantity' => 1,
'variant_id' => 42837938757,
'vendor'=>'Saaraketha Organics',
'product_id'=>9043955845
),
)
)
);
$order = json_encode ($orderdata);
$logcontent = "$order\n";
file_put_contents("php://stderr", $logcontent);
//create client and post data
$url =(string)('https://api_key:password#domain/admin/orders.json');
$client = new Client();
$RequestResponse = $client->post($url, ['headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json'], 'body' => $order]);
Although it successfully creates orders, it repeatedly occurs.
This is the Heroku log I got.
Log
I've figured it out.
The app is being called on Order Creation webhook. Within the same app controller it post orders, which again triggers same webhook, making an infinite sequence of requests.
AWS SNS Push notification is not working for android GCM in my application though same code is working pretty good for IOS. I put cloudwatch to track down the error but it displaying status as delivered in logs but no push notification received in Device.
Relevant code of application
$platform = 1;
$token = 'user-device-token';
$gcmPlatformArn = 'AWS_GCM_ARN';
$ApnsPlatformArn = 'AWS_IOS_ARN';
$message = 'welcome';
$sns = SnsClient::factory([
'region' => 'my-region',
'version' => 'latest',
'credentials' => [
'key' => 'my-key',
'secret' => 'my-secret'
]
]);
if ($platform == 1) { //For Android
$endPointArn = $sns->createPlatformEndpoint([
'PlatformApplicationArn' => $gcmPlatformArn,
'Token' => $token
]);
} else {
$endPointArn = $sns->createPlatformEndpoint([
'PlatformApplicationArn' => $ApnsPlatformArn,
'Token' => $token
]);
}
$endPoint = $endPointArn['EndpointArn'];
return $sns->publish(['Message' => $message,
'TargetArn' => $endPoint]);