Hướng dẫn composer require aws/aws-sdk-php

You can install the AWS SDK for PHP Version 3:

  • As a dependency via Composer

  • As a prepackaged phar of the SDK

  • As a ZIP file of the SDK

Before you install AWS SDK for PHP Version 3 ensure your environment is using PHP version 5.5 or later. Learn more about environment requirements and recommendations.

Install AWS SDK for PHP as a dependency via Composer

Composer is the recommended way to install the AWS SDK for PHP. Composer is a tool for PHP that manages and installs the dependencies of your project.

For more information on how to install Composer, configure autoloading, and follow other best practices for defining dependencies, see getcomposer.org.

Install Composer

If Composer is not already in your project, download and install Composer.

For Windows, download and run the Composer-Setup.exe.

For Linux, follow the Command-line installation on the Download Composer page.

Add AWS SDK for PHP as a dependency via Composer

If Composer is already installed globally on your system, run the following in the base directory of your project to install AWS SDK for PHP as a dependency:

composer require aws/aws-sdk-php

Otherwise type this Composer command to install the latest version of the AWS SDK for PHP as a dependency.

php -d memory_limit=-1 composer.phar require aws/aws-sdk-php

Add autoloader to your php scripts

To utilize the AWS SDK for PHP in your scripts, include the autoloader in your scripts, as follows.

Installing by using the packaged phar

Each release of the AWS SDK for PHP includes a prepackaged phar (PHP archive) that contains all the classes and dependencies you need to run the SDK. Additionally, the phar automatically registers a class autoloader for the AWS SDK for PHP and all its dependencies.

You can download the packaged phar and include it in your scripts.

Using PHP with the Suhosin patch is not recommended, but is common on Ubuntu and Debian distributions. In this case, you might need to enable the use of phars in the suhosin.ini. If you don’t do this, including a phar file in your code will cause a silent failure. To modify suhosin.ini, add the following line.

suhosin.executor.include.whitelist = phar

Installing by using the ZIP file

The AWS SDK for PHP includes a ZIP file containing all the classes and dependencies you need to run the SDK. Additionally, the ZIP file includes a class autoloader for the AWS SDK for PHP and its dependencies.

To install the SDK, download the .zip file, and then extract it into your project at a location you choose. Then include the autoloader in your scripts, as follows.

There are a number of ways to install the AWS SDK for PHP. This topic covers:

  • Installing using Composer (recommended)
  • Installing using the PHP archive (.phar)
  • Installing using the .zip archive

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:

  1. Prefer not to or cannot use Composer.
  2. Cannot use .phar files due to environment limitations.
  3. Want to use only specific files from the SDK.

Dependencies¶

The AWS SDK for PHP depends on the following libraries:

  • Guzzle for HTTP requests
  • Symfony2 EventDispatcher for events
  • Monolog and Psr\Log for logging
  • Doctrine for caching

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.

Installing using Composer¶

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:

  1. Open a terminal window and navigate to the directory where your project is stored. Composer is installed on a per-project basis.

  2. 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.

  3. 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.*"
        }
    }
    

  4. 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.

  5. 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!

Using Composer with AWS Elastic Beanstalk¶

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.

Keeping up to date with SDK changes¶

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.

Installing using the PHP archive (.phar)¶

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

Installing using the .zip archive¶

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.