Menu
Amazon DynamoDB
Developer Guide (API Version 2012-08-10)

PHP Code Samples

This Developer Guide contains PHP code snippets and ready-to-run programs. You can find these code samples in the following sections:

Note

The Amazon DynamoDB Getting Started Guide contains additional PHP sample programs.

The way you use the SDK for PHP depends on your environment and how you want to run your application. The code samples in this document run from the command line, but you can modify them if you want to run them in a different environment (such as a web server).

To Run the PHP Code Samples (from the command line)

  1. Verify that your environment meets the minimum requirements for the SDK for PHP, as described in Minimum Requirements.

  2. Download and install the AWS SDK for PHP.

    Depending on the installation method that you use, you might have to modify your code to resolve dependencies among the PHP extensions. For more information, see the Getting Started section of the Getting Started in the AWS SDK for PHP documentation.

  3. Copy the code sample (from the documentation page you are reading) into a file on your computer.

  4. Run the code from the command line. For example: php myProgram.php

Note

The code samples in this Developer Guide are intended for use with the latest version of the AWS SDK for PHP.

PHP: Setting Your AWS Credentials

The SDK for PHP requires that you provide AWS credentials to your application at runtime. The code samples in this Developer Guide assume that you are using an AWS credentials file, as described in Credentials in the AWS SDK for PHP documentation.

The following is an example of an AWS credentials file named ~/.aws/credentials, where the tilde character (~) represents your home directory:

[default] 
aws_access_key_id = AWS access key ID goes here 
aws_secret_access_key = Secret key goes here         

PHP: Setting the AWS Region and Endpoint

You must specify an AWS region when you create a DynamoDB client. To do this, you provide the Aws\Sdk object with the region you want.

The following code snippet instantiates a new Aws\Sdk object, using the US West (Oregon) region. It then creates a DynamoDB client that uses this region.

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2', // US West (Oregon) Region
    'version'  => 'latest'  // Use the latest version of the AWS SDK for PHP
]);

// Create a new DynamoDB client
$dynamodb = $sdk->createDynamoDb();

If you want to run the code samples using DynamoDB locally on your computer, you need to set the endpoint, as shown following:

$sdk = new Aws\Sdk([
    'endpoint'   => 'http://localhost:8000',  // Use DynamoDB running locally
    'region'   => 'us-west-2',  // US West (Oregon) Region
    'version'  => 'latest'  // Use the latest version of the AWS SDK for PHP
]);

// Create a new DynamoDB client
$dynamodb = $sdk->createDynamoDb();