[英]Amazon SNS to push topic using PHP
我正在尝试了解适用于 AWS 的 amazon php sdk,但我真的无法使用它。 我找到了一些基本的类来创建、显示和推送主题,但我也不工作。 我只想找到一种方法(最简单的方法)从我的网站推送主题。
首先,为了熟悉Amazon Simple Notification Service (SNS) ,我建议通过AWS 管理控制台手动执行所有必需的步骤,如入门指南中所述,即创建主题、订阅此主题并 发布一个给它留言。
之后,它应该相当简单地促进运行AWS SDK for PHP的文档中提供的示例片段,参见例如 AmazonSNS 类中的方法publish() :
$sns = new AmazonSNS();
// Get topic attributes
$response = $sns->publish(
'arn:aws:sns:us-east-1:9876543210:example-topic',
'This is my very first message to the world!',
array(
'Subject' => 'Hello world!'
)
);
// Success?
var_dump($response->isOK());
有关更完整的示例,您可能需要查看使用 SNS 发送消息 [...] 中提供的示例。
如果这些都不起作用,您将必须提供有关您遇到的特定问题的更多详细信息,正如tster已经要求的那样。
祝你好运!
正如@SteffenOpel 告诉您的那样,您应该尝试一次通过 AWS 管理控制台手动执行所有必需的步骤。
然后,您可以使用AWS SDK for PHP (v3) as below
创建 SNS 客户端(或实际上任何服务的客户端,当然有一些更改),然后创建 SNS Topic 。
<?php
//assuming that use have downloaded the zip file for php sdk
require 'C:/wamp/www/aws sdk/aws-autoloader.php'; //Change the path according to you
use Aws\Sns\SnsClient;
try{
/*-------------METHOD 1----------------*/
// Create a new Amazon SNS client using AWS v3
//$sns = new Aws\Sns\SnsClient([
$sns = new SnsClient([
'region' => 'us-west-2', //Change according to you
'version' => '2010-03-31', //Change according to you
'credentials' => [
'key' => '<Your root AWS Key',
'secret' => '<Your root AWS Secret>',
],
'scheme' => 'http', //disables SSL certification, there was an error on enabling it
]);
$result = $sns -> createTopic([
'Name' => '<Your Topic>',
]);
/*-------------METHOD 2----------------*/
/*
// Create a new Amazon SNS client using AWS v2
$sns = SnsClient::factory(array(
'region' => 'us-west-2',
'version' => '2010-03-31',
'credentials' => [
'key' => '<Your root AWS Key',
'secret' => '<Your root AWS Secret>',
],
'scheme' => 'http',
));
$result = $sns -> createTopic([
'Name' => '<Your Topic>',
]);
*/
/*-------------METHOD 3----------------*/
/*
// Create a new Amazon SNS client using AWS SDK class
// Use the us-west-2 region and latest version of each client.
$sharedConfig = [
'region' => 'us-west-2',
'version' => '2010-03-31',
'credentials' => [
'key' => '<Your root AWS Key',
'secret' => '<Your root AWS Secret>',
],
//'ssl.certificate_authority' => '/path/to/updated/cacert.pem',
'scheme' => 'http',
];
// Create an SDK class used to share configuration across clients.
$sdk = new Aws\Sdk($sharedConfig);
$sns = $sdk -> createSns();
$result = $sns -> createTopic([
'Name' => '<Your Topic>',
]);
*/
if ($result)
echo "Yes";
else
echo "No";
}
catch(Exception $e){
echo 'Caught Exception: ', $e->getMessage(), "\n";
}
?>
注意:此代码说明了以三种不同的方法为 SNS 创建客户端。 您可以根据需要取消注释并使用一个。 如果您正在创建单个客户端,方法 1(版本 3)是最好的,否则使用方法 3。方法 2 很快就会贬值(作为它的版本 2)
我通过使用这个类成功做到了 -> Amazon-SNS-client-for-PHP
非常好,易于使用且工作得很好。
根据此处的 AWS 官方文档,我们必须创建一个 SnsClient 并调用它的发布方法。 您可以从 AWS 控制台获取主题的 ARN。
$SnSclient = new SnsClient([
'profile' => 'default',
'region' => 'us-east-1',
'version' => '2010-03-31'
]);
$message = 'This message is sent from a Amazon SNS code sample.';
$topic = 'arn:aws:sns:us-east-1:111122223333:MyTopic';
try {
$result = $SnSclient->publish([
'Message' => $message,
'TopicArn' => $topic,
]);
var_dump($result);
} catch (AwsException $e) {
// output error message if fails
error_log($e->getMessage());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.