Install the OSS SDK for PHP to manage buckets and objects in Object Storage Service (OSS). Three installation methods are available: Composer, PHAR file, and source code.
Prerequisites
Before you begin, ensure that you have:
PHP 5.3 or later (this document uses PHP 5.6.22 as an example)
The cURL extension for PHP
Install the cURL extension
Windows
See Compile and use Alibaba Cloud OSS PHP SDK on Windows to install PHP and the cURL extension.
If a "module not found" error occurs, set extension_dir to C:/Windows/System32/ in your php.ini file.
Ubuntu
sudo apt-get install php-curlCentOS
sudo yum install php-curlVerify prerequisites
Run the following commands to confirm that PHP and cURL are installed:
# Check PHP version
php -v
# List loaded extensions
php -mThe output of php -v displays the PHP version. The output of php -m should include curl.
Choose an installation method
| Method | Best for |
|---|---|
| Composer (recommended) | Projects that use a dependency manager |
| PHAR file | Projects without Composer |
| Source code | Custom builds or legacy systems |
Method 1: Composer (recommended)
Composer is the standard PHP dependency manager. Use this method for most projects.
Step 1: Install Composer
If Composer is not already installed:
Windows: Follow the Windows Installer instructions.
Linux/macOS: Follow the command-line installation instructions.
Step 2: Add the SDK dependency
In the root directory of your project, run:
composer require aliyuncs/oss-sdk-phpAlternatively, add the dependency to your composer.json file and run composer install:
"require": {
"aliyuncs/oss-sdk-php": "~2.4"
}Step 3: Add the autoloader
After installation, your project structure looks like this:
.
├── src
| └── app.php
├── composer.json
├── composer.lock
└── vendorAdd the Composer autoloader to your application file (for example, app.php):
<?php
require_once __DIR__ . '/../vendor/autoload.php';
?>If your project already references autoload.php, skip this step.
If a network error occurs, use the Composer mirror for the China region by running: composer config -g repositories.packagist composer http://packagist.phpcomposer.comMethod 2: PHAR file
Go to GitHub Releases and download the PHAR file for your target version.
Include the PHAR file in your code:
<?php require_once '/path/to/oss-sdk-php.phar'; ?>
Method 3: Source code
Go to GitHub Releases and download the ZIP file for your target version.
Extract the archive. The root directory contains an
autoload.phpfile. Include it in your code:<?php require_once '/path/to/oss-sdk/autoload.php'; ?>
Verify the SDK installation
Create a test script to confirm the SDK is installed correctly:
<?php
require_once __DIR__ . '/vendor/autoload.php';
use OSS\OssClient;
echo "OSS SDK loaded successfully.\n";
?>Run the script:
php test.phpIf the SDK is installed correctly, the script runs without errors.
Troubleshooting
"Your configuration does not allow connection" error
Error message
Your configuration does not allow connection to http://packagist.phpcomposer.com/packages.json.
See https://getcomposer.org/doc/06-config.md#secure-http for detailsCause
Composer blocks HTTP connections by default and requires HTTPS for all package downloads.
Solution
If HTTPS is unavailable in your network environment, allow HTTP connections by running the following command in your project root:
composer config secure-http falseDisabling HTTPS reduces security. Use secure connections whenever possible.
cURL extension not loaded
Symptom
Call to undefined function curl_init()Cause
The cURL extension is not installed or not enabled.
Solution
Install the cURL extension for your operating system (see Prerequisites).
Verify the extension is loaded:
php -m | grep curlIf cURL is installed but not listed, uncomment the following line in
php.ini:extension=curlRestart your web server or PHP-FPM service.
SDK resources
| Resource | Link |
|---|---|
| Source code | GitHub |
| Releases and changelog | GitHub Releases |
| API reference | OSS PHP SDK API documentation |
Use the latest version of the SDK. For versions earlier than 2.0.0, see OSS PHP SDK legacy documentation.
What's next
Initialize an OSS client — Configure access credentials and create an OSS client.
Quick start — Upload and download your first object.