| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
The general process for running a PHP code example is as follows. The individual steps are explained in greater detail in the following sections.
General Process of Creating PHP Code Examples
|
1 | Download and install the AWS SDK for PHP, and then verify that your environment meets the minimum requirements as described in the AWS SDK for PHP 2 Getting Started Guide. |
|
2 |
Install the AWS SDK for PHP according to the instructions in the AWS SDK for PHP 2 Getting Started Guide. Depending on the installation method that you use, you might have to modify your code to resolve dependencies among the PHP extensions. All of the PHP code samples in this document use the Composer dependency manager that is described in the AWS SDK for PHP 2 Getting Started Guide. Each code sample includes the following line to manage dependencies: require 'vendor/autoload.php'; |
|
3 |
Copy the example code from the document to your project. Depending upon your environment, you might need to add lines to the code example that reference your configuration and SDK files. For example, to load a PHP example in a browser, add the following
to the top of the PHP code, and then save it as a PHP file
(extension
<?php
header('Content-Type: text/plain; charset=utf-8');
// Include the AWS SDK using the Composer autoloader
require 'vendor/autoload.php';
|
|
4 |
Test the example according to your setup. |
For security reasons, we recommend that you create a configuration file that
contains your AWS access key ID and secret key. This approach will let you avoid
hardcoding your credentials in the PHP code itself. At run time, when you create a
new DynamoDb object, the client can obtain its credentials from the
configuration file.
The following is an example of such a configuration file, named
config.php:
<?php
return array(
'includes' => array('_aws'),
'services' => array(
'default_settings' => array(
'params' => array(
'key' => 'AWS access key ID goes here',
'secret' => 'Secret key goes here'
)
)
)
);
?> Here is how to use config.php to instantiate a new client:
use Aws\DynamoDb\DynamoDbClient;
/
/ Instantiate the DynamoDB client with your AWS credentials
$aws = Aws\Common\Aws::factory('./config.php');
$client = $aws->get('dynamodb');
For more information about setting up a configuration file, go to the "Configuration / Service Builder" section in the AWS SDK for PHP 2 Migration Guide.
If you are using a configuration file, you can override the default region for your client, as in the following example:
<?php
return array(
'includes' => array('_aws'),
'services' => array(
'default_settings' => array(
'params' => array(
'key' => 'AWS access key ID goes here',
'secret' => 'Secret key goes here',
'region' => 'us-west-2' // US West (Oregon)
)
)
)
);
?> You can also set the region at run time by modifying your DynamoDb client:
use Aws\Common\Enum; $client->setRegion(Aws\Common\Enum\Region::EU_WEST_1); // EU (Ireland)
For a current list of supported regions and endpoints, see Regions and Endpoints.