| « PreviousNext » | |
![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
To get started with the AWS SDK for PHP, you need to set up the following:
AWS Account and Credentials
PHP Development Environment
AWS SDK for PHP
To access AWS, you will need to sign up for an AWS account.
To sign up for an AWS account
Go to http://aws.amazon.com, and then click Sign Up.
Follow the on-screen instructions.
Part of the sign-up procedure involves receiving a phone call and entering a PIN using the phone keypad.
AWS sends you a confirmation e-mail after the sign-up process is complete. At any time, you can view your current account activity and manage your account by going to http://aws.amazon.com and clicking My Account/Console.
When you sign up, AWS provides you with security credentials that are specific to your account. Two of these credentials, your access key ID and your secret key, are used by the SDK whenever it accesses the services provided by AWS. The security credentials authenticate requests to the service and identify you as the sender of the request. Examples of these credentials are shown below.
Access Key ID Example: AKIAIOSFODNN7EXAMPLE
Secret Access Key Example: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
To view your AWS access credentials
Go to the Amazon Web Services website at http://aws.amazon.com.
Click My Account/Console, and then click Security Credentials.
Under Your Account, click Security Credentials.
In the spaces provided, type your user name and password, and then click Sign in using our secure server.
Under Access Credentials, on the Access Keys tab, your access key ID is displayed. To view your secret key, under Secret Access Key, click Show.
Your secret key must remain a secret that is known only by you and AWS. Keep it confidential in order to protect your account. Store it securely in a safe place, and never email it. Do not share it outside your organization, even if an inquiry appears to come from AWS or Amazon.com. No one who legitimately represents Amazon will ever ask you for your secret key.
Aside from a baseline understanding of object-oriented programming in PHP, there are a few minimum requirements to start using the AWS SDK for PHP. All the following requirements are common in PHP environments, and most extensions come installed with PHP 5.2 by default.
The minimum requirements are:
The cURL extension should be compiled with the OpenSSL libraries for HTTPS support.
The ability to read from, and write to, the file system via file_get_contents and file_put_contents.
If you're not sure if your PHP installation supports these requirements, you can run the AWS SDK for PHP Compatibility Test.
Alternatively, you can check by either running phpinfo() in a browser or by running php -i from the command line.)
To install your PHP environment, choose one of the installations listed below that corresponds to the operating system that you are using. If you are running PHP on an Amazon EC2 instance, you should choose an installation based on the operating system that the Amazon EC2 instance is running. Each operating system shown below lists multiple possible installations. For example, for Linux, you could install any one of Apache Friends XAMPP, BitName LAMPStack, or BitNami LAPPStack. Click through the links for more information and to determine the most appropriate installation for your needs.
Depending on your distribution, you can likely install everything you need via yum or apt-get;
If you are using an Amazon EC2 instance based on an Amazon Linux AMI, you can install PHP with PEAR and APC using yum with a command such as the following.
# Install PHP 5.4 (w/ PEAR and APC) sudo yum install php54 php54-pecl-apc
To use Amazon CloudFront, you must have the OpenSSL PHP extension (which is different from any low-level OpenSSL libraries installed on your system) to sign private CloudFront URLs.
To use response caching with the following tools, you must install the associated extensions:
Alternative PHP Cache (APC)—APC PHP extension
XCache—XCache PHP extension
Memcache—Either the older Memcache PHP extension, or the newer Memcached PHP extension
SQLite—If the PHP Data Objects (PDO) extension support isn't already enabled, install the SQLite3 PHP extension and the PDO-SQLite driver
There are a number of ways that you can install the AWS SDK for PHP. Each option is described below.
Download the install file from Amazon Web Services website http://aws.amazon.com/sdkforphp
Install from GitHub.
git clone git://github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP cd ./AWSSDKforPHP
For more information on Git, explore Git for the lazy, or Pro Git.
Install from Subversion
svn co http://svn.github.com/amazonwebservices/aws-sdk-for-php.git AWSSDKforPHP cd ./AWSSDKforPHP
Install from PEAR
The following example shows how to install v1.6.0 of the SDK. To install a
different version replace -1.6.0 with the version to install.
# Configure PEAR to auto-discover the channels of dependencies sudo pear config-set auto_discover 1 # Install the SDK sudo pear channel-discover pear.amazonwebservices.com sudo pear install aws/sdk-1.6.0
To view the available package versions, see the Amazon Web Services PEAR Channel.
To verify that PEAR installed the SDK successfully, you can use the following command line, which should print the SDK version.
php -r "require 'AWSSDKforPHP/sdk.class.php'; echo CFRUNTIME_VERSION.PHP_EOL;"
There are two different ways to configure your credentials: one approach is geared for simplicity, whereas the other is geared for flexibility. The simple approach is preferred for all the installation approaches described above—except for PEAR. For PEAR installations, the flexible approach is preferred. You can find a copy of the config-sample.inc.php file referenced below on GitHub.
To configure your AWS credentials for a non-PEAR installation.
Be sure to back up your config.inc.php file when upgrading to avoid having to re-enter your credentials.
Navigate to the config-sample.inc.php file in the SDK directory, and rename it to config.inc.php.
Open config.inc.php for editing. There are instructions in this file for each configuration value.
When the SDK is loaded, it will look for config.inc.php in the same directory as sdk.class.php. You are now ready to run PHP code that accesses AWS.
To configure your AWS credentials for a PEAR installation.
Create a new configuration file at ~/.aws/sdk/config.inc.php.
Either download and copy the contents of a config-sample.inc.php file, or grab it directly from the above GitHub link.
Open config.inc.php for editing. There are instructions in this file for what each configuration value.
Enter
echo getenv('HOME');
to verify that PHP can access the HOME environment variable. If this command doesn't return the correct file path to your user directory, configure it with:
putenv('HOME=<your-user-path>');.
The AWS SDK for PHP contains sample code that demonstrates how to use the AWS SDK for PHP with Amazon S3. The sample creates an Amazon S3 bucket, uploads a number of files from a test-files/ directory in to the bucket, then displays a URL for each file.
Note Before you run the sample you must configure the SDK with your AWS security credentials as described above.
To run the Amazon S3 Sample, run the cli-s3_get_urls_for_uploads.php file located in the samples folder of the SDK installation directory.
Where Do I Go From Here?
From here, you can check out the tutorials included in this developer guide.
Tutorial: Player Manager
The first tutorial explains how to use Amazon S3 and Amazon SimpleDB to store information about a group of people—such as players at a tennis club.
Tutorial: Amazon EC2 Spot Instances
The tutorial explains how to set up a requests for Amazon EC2 Spot Instances, how to determine when they have completed, and how to clean up afterwards.
SDK Reference Documentation
The SDK reference documentation includes the ability to browse and search across all code included with the SDK. It provides thorough documentation, usage examples, and even the ability to browse method source. You can find it at http://docs.aws.amazon.com/AWSSDKforPHP/latest.
Additional Resources
The Further Resources has pointers to other resources to assist you in programming AWS.