Giter Club home page Giter Club logo

amazonsns-php-api's Introduction

Amazon SNS PHP API

Latest Stable Version PHP from Packagist

This API wrapper is a lightweight alternative to the official Amazon aws-sdk-for-php for access to Amazon SNS (Simple Notification Service) using PHP

Find out more about Amazon SNS here - http://aws.amazon.com/sns

To use this wrapper you must be using PHP5 with cURL, and have an Amazon AWS account

Basic Use

Install using Composer on the command line:

$ composer require chrisbarr/amazon-sns-php-api

Or add it to your composer.json file:

{
	...
	"require": {
		"chrisbarr/amazon-sns-php-api": "~1.0"
	}
}

Example usage:

<?php
require 'vendor/autoload.php';

// Create an instance
$AmazonSNS = new AmazonSNS(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_ACCESS_KEY);

// Create a Topic
$topicArn = $AmazonSNS->createTopic('My New SNS Topic');

// Set the Topic's Display Name (required)
$AmazonSNS->setTopicAttributes($topicArn, 'DisplayName', 'My SNS Topic Display Name');

// Subscribe to this topic
$AmazonSNS->subscribe($topicArn, 'email', '[email protected]');

// And send a message to subscribers of this topic
$AmazonSNS->publish($topicArn, 'Hello, world!');

API Methods

Available methods:

  • addPermission($topicArn, $label, $permissions)
  • confirmSubscription($topicArn, $token)
  • createTopic($name)
  • deleteTopic($topicArn)
  • getTopicAttributes($topicArn)
  • listSubscriptions()
  • listSubscriptionsByTopic($topicArn)
  • listTopics()
  • publish($topicArn, $message, $subject, $messageStructure)
  • removePermission($topicArn, $label)
  • setTopicAttributes($topicArn, $attrName, $attrValue)
  • subscribe($topicArn, $protocol, $endpoint)
  • unsubscribe($subscriptionArn)
  • createPlatformEndpoint($platformApplicationArn, $token, $userData)
  • deleteEndpoint($deviceArn)
  • publishToEndpoint($deviceArn,$message)

To set the API region (us-east-1, us-west-2, us-west-1, eu-west-1, etc):

  • setRegion($region)

The default API region is us-east-1

Further Example

Make sure to catch Exceptions where necessary:

<?php
require 'vendor/autoload.php';

$AmazonSNS = new AmazonSNS(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_ACCESS_KEY);
$AmazonSNS->setRegion('eu-west-1');

try {
	$topics = $AmazonSNS->listTopics();
}
catch(SNSException $e) {
	// Amazon SNS returned an error
	echo 'SNS returned the error "' . $e->getMessage() . '" and code ' . $e->getCode();
}
catch(APIException $e) {
	// Problem with the API
	echo 'There was an unknown problem with the API, returned code ' . $e->getCode();
}

amazonsns-php-api's People

Contributors

chrisbarr avatar rohit-wadhwa avatar techjewel avatar vis4573 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

amazonsns-php-api's Issues

There was a problem executing this request

I have a progrma in the format of this and I am getting this error There was a problem executing this request. Could you halp me understand the problem.

// Create an instance
$AmazonSNS = new AmazonSNS('key', 'key');
$AmazonSNS->setRegion('us-east-1');

// And send a message to subscribers of this topic
$AmazonSNS->publish(topicarn', $message);

Problem with http_build_query

Not sure why, but on one of our servers http_build_query() function was separating URL parameters with &amps; instead of &.

Changing line to this solved the issue.
$request = $this->protocol . $this->endpoint . '/?' . http_build_query($params,'','&');

Second issue I found was that the Timestamp should include milliseconds:
$params['Timestamp'] = gmdate('Y-m-d\TH:i:s.000\Z');

Otherwhise great class. I love these simple single purpose classes instead of large SDKs.

listSubscriptions() or listSubscriptionsByTopic() do not work with the returned NextToken from previous call

  1. the NextToken does not get returned by these functions, a bit hard if you want your next request to pass the returned NextToken param, so might be better to return it ;)
if(isset($resultXml->ListSubscriptionsResult->NextToken)) {
    $return->nexttoken = $resultXml->ListSubscriptionsResult->NextToken;
}
$return->members = $this->_processXmlToArray($subs);
return $return;
  1. Even when you change the return of these functions and use the returned NextToken to pass into the next call, AWS returns error: "SNS returned the error "SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details." and code 403".

Something goes wrong in the Signature creation... Any ideas?

Why does function subscribe returns true?

Upon unsubscribe, you need the subscriptionArn which is returned by AmazonSNS upon subscribe. If you return true, the subscriptionArn cannot be saved and thus the Endpoint cannot be unsubscribed afterwards

Fatal error: Uncaught exception 'APIException' with message 'There was a problem executing this request'

Run your sample code, got this error:

Fatal error: Uncaught exception 'APIException' with message 'There was a problem executing this request' in /Applications/XAMPP/xamppfiles/htdocs/dummyserver/vendor/chrisbarr/amazon-sns-php-api/lib/AmazonSNS.php:567 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/dummyserver/vendor/chrisbarr/amazon-sns-php-api/lib/AmazonSNS.php(177): AmazonSNS->_request('CreateTopic', Array) #1 /Applications/XAMPP/xamppfiles/htdocs/dummyserver/server1/sns.php(9): AmazonSNS->createTopic('My New SNS Topi...') #2 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/dummyserver/vendor/chrisbarr/amazon-sns-php-api/lib/AmazonSNS.php on line 567

Hardcoded endpoints

I've been using this library for a while. But I now have the requirement to connect to a Dockerised local version of SNS (rather than the real live AWS service).

Obviously, it's impossible with this library because the endpoints are derived from the region in the constructor. I'm currently working on adapting this library so the constructor will look like this:

public function __construct(string $accessKey, string $secretKey, string $endpoint) {

I will then provide a factory method to construct the client using a region:

public static function fromAwsCredentials(string $accessKey, string $secretKey, string $region): AmazonSNS {

This method would then encapsulate the logic of converting the region into an endpoint. I'd probably also inject the functionality of converting the region into an endpoint using a RegionHandler class or something so that users can create their own providers (for handling new regions or whatever).

Obviously this is making a breaking change and is almost become another library altogether. But I will keep your _request method and all the API methods themselves intact.

So my question is, do you want me to do a PR for all of this? And make it a new major version? Or would you rather I just created my own GitHub/Packagist for this and go our separate ways?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.