AWS Official Blog

New AWS SDK for PHP

by Jeff Barr | on | in Developer Tools |

We’ve got a really nice new AWS SDK for PHP. Like our existing .NET and Java toolkits, this one was designed to be a high-quality SDK with comprehensive feature coverage, documentation, and tutorials. The first release supports a large subset of our infrastructure services including the Amazon Elastic Compute Cloud (EC2), the Amazon Simple Storage Service (S3), Amazon CloudFront, Amazon CloudWatch, Amazon SimpleDB, the Amazon Simple Notification Service (SNS), the Amazon Simple Queue Service (SQS), as well as  Amazon Identify and Access Management (IAM).

The new SDK is a derivative of the popular and highly respected CloudFusion toolkit. In fact, the lead CloudFusion developer is now a part of our Developer Resources team (we’ve got another opening on that team if you are interested)!

Building the SDK in-house means that we’ll be able to deliver updates simultaneously with updates to the services. Having more PHP developers on staff means that we’ll be in a position to help with PHP-related questions and issues on the AWS Forums.;

The AWS SDK for PHP includes the following goodies:

  • AWS PHP Libraries – Build PHP applications using APIs that take the complexity out of coding directly against a web service interface. The toolkit provides APIs that hide much of the lower-level plumbing, including authentication, signatures, and error handling.
  • Code Samples – Practical examples showing how to use the toolkit to build real applications.
  • Documentation – Complete, interactive SDK reference documentation with embedded samples.
  • PEAR Package – You can install the AWS SDK automatically using the PHP Extension & Application Repository (PEAR).

Here’s what the documentation browser looks like:

We’ve also set up a dedicated forum for PHP developers. You can find the AWS SDK for PHP, links to the forums, and additional resources for PHP developers in our revised and expanded PHP Developer Center.

I wrote a little application to experiment with the new Toolkit and to get some experience with Amazon SNS. Using the SDK is as simple as including one file. Once included, I create an access object so that I can make calls to SNS:

#!/usr/bin/env php
require_once(“ sdk.class.php“);
// Create the SNS object
$sns = new AmazonSNS();

Then I create some new SNS topics. This is an idempotent operation, so there’s no harm in trying to re-create a topic that already exists. I am using a batch operation for greater efficiency.

// Create some new topics
foreach (array(‘currency_usd’, ‘currency_jpy’, ‘currency_brl’) as $Topic)
{
$sns->batch()-> create_topic($Topic);
}
$sns-> batch()->send();

I can now call SNS’s get_topic_list function to discover all of my topics:

// Gather up all of the topics
$topics = $SNS-> get_topic_list();

Then I subscribe my email address to each topic. SNS will send a confirming email after each request. I must click on a link in the email to confirm my intent to subscribe.

// Subscribe an email address to each topic
foreach ($topics as $topic)
{
$response = $SNS-> subscribe($topic, “email”, “ my_email_address@amazon.com”);
print(“Subscribe to ${topic}: ” . ($response->isOK() ? “succeed” : “fail”) . PHP_EOL);
}

With topics created and subscriptions established, I can now publish notifications with ease:

// Publish something to each topic foreach ($topics as $topic) {
$rand1 = rand(1, 100);
$rand2 = rand(1, 100);
$response = $SNS-> publish($Topic, “Start: ${rand1}, End: ${rand2}\n”,
array(“Subject” => “Price Change”));
}

Now that this toolkit has been released, I will make some minor changes to the code samples in my new AWS book and make the code available through the SitePoint code archive in the very near future.

– Jeff;