There are a number of ways to install the AWS SDK for PHP. This topic covers:
Using Composer is the most flexible approach, since all dependencies will be automatically fetched for you, and you can specify a particular SDK version, range of versions, or use the latest development version. It is also the easiest way to deploy your code to the AWS cloud using Elastic Beanstalk.
Using the .zip archive is useful if you:
The AWS SDK for PHP depends on the following libraries:
These libraries are automatically fetched for you if you use Composer, but are also included in the .phar and .zip archives if you choose either of those installation options.
Using Composer is the recommended way to install the AWS SDK for PHP. Composer is a dependency management tool for PHP that allows you to declare the dependencies your project needs, and then automatically installs them into your project.
Tip
You can find out more on how to install Composer, configure autoloading, and other best-practices for defining dependencies at getcomposer.org.
To use Composer with the AWS SDK for PHP:
Open a terminal window and navigate to the directory where your project is stored. Composer is installed on a per-project basis.
Download and install Composer in your project directory. If you have curl
installed, you can
use the following command:
curl -sS https://getcomposer.org/installer | php
Otherwise, follow the installation instructions provided in the Composer documentation.
When the installation script finishes, a composer.phar
file will be created in the directory
where you ran the installer.
Create a file at the root level of your project called composer.json
and add the following
dependency for the AWS PHP SDK:
{
"require": {
"aws/aws-sdk-php": "2.*"
}
}
Install the dependencies by running Composer's install
command:
php composer.phar install
This will create a vendor
directory in your project with the required libraries and an
autoloader script used to load them for your project.
Require Composer's autoloader by adding the following line to your code's bootstrap process
(typically in index.php
):
require '/path/to/sdk/vendor/autoload.php';
Your code is now ready to use the AWS SDK for PHP!
If you deploy your application using AWS Elastic Beanstalk and you have a composer.json
file
in the root of your package, then Elastic Beanstalk will automatically install Composer for you when
you deploy your application.
During development of your application, you can keep up with the latest changes on the master branch
by setting the version requirement for the SDK to dev-master
.
{
"require": {
"aws/aws-sdk-php": "dev-master"
}
}
Before releasing your code, consider restricting your dependencies to a specific SDK version or a known-good set of versions to reduce any issues with SDK feature compatibility.
For more information about how to specify dependency versions, see The require Key in the Composer documentation.
Each release of the AWS SDK for PHP provides a PHP archive (phar) that contains the SDK and all of the classes and dependencies you need to run the SDK. Additionally, the phar file automatically registers a class autoloader for the AWS SDK for PHP and all of its dependencies when it is included.
You can download specific versions of the AWS SDK for PHP .phar from https://github.com/aws/aws-sdk-php/releases. To use it, simply include it in your scripts:
require '/path/to/aws.phar';
Note
If you are using PHP with the Suhosin patch (especially common on Ubuntu and Debian
distributions), you may need to enable the use of phars in the suhosin.ini
file. Without
this, including a phar file in your code will cause it to silently fail. You should modify
suhosin.ini
by adding the line:
suhosin.executor.include.whitelist = phar
Each release of the AWS SDK for PHP since version 2.3.2 provides a zip file containing all of the classes and dependencies that you need to run the SDK in a PSR-0 compatible directory structure.
To get started, download a specific version of the zip file from https://github.com/aws/aws-sdk-php/releases, unzip it into your project to a location of your choosing, and include the autoloader:
require '/path/to/aws-autoloader.php';
Alternatively, you can write your own autoloader or use an existing one from your project.
If you have phing installed, you can clone the SDK and build a zip file yourself using the "zip" task.