PHP SDK

更新时间:
复制 MD 格式

Use Tablestore SDK for PHP to perform wide column model operations.

Quick start

Prepare your environment, install the SDK, and initialize a client.

Prepare the environment

  • 64-bit PHP 5.5 or later. Run php -v to check your version.

    Note

    Tablestore uses 64-bit integers. 32-bit PHP can only represent them as string values and is not supported. On Windows, PHP versions earlier than 7 lack true 64-bit integer support. Use PHP 7 or later.

  • If you use HTTPS, install the OpenSSL PHP extension.

  • A PHP IDE such as Eclipse for PHP.

  • The cURL extension (recommended). Run php -m to verify installation.

Install the SDK

Install the SDK with Composer or from a source package.

Composer

To install with Composer:

  1. Run composer require aliyun/aliyun-tablestore-sdk-php in the project root, or add the dependency to composer.json.

    Note

    If you encounter network errors with Composer, use the China region mirror. Run composer config -g repo.packagist composer https://developer.aliyun.com/composer in the command line.

    {
        "require": {
            "aliyun/aliyun-tablestore-sdk-php": "^5.1"
        }
    }
  2. Run composer install. The resulting directory structure:

     .
     ├── app.php
     ├── composer.json
     ├── composer.lock
     └── vendor

    vendor/ contains the installed libraries. Include the autoloader in your application:

    require_once __DIR__ . '/vendor/autoload.php';
    Note

    Skip this step if your project already includes autoload.php.

Source package

Download the source package:

The SDK includes sample programs for common wide column model operations:

  • Download and extract the SDK package. Samples are in the examples directory.

  • Browse the aliyun-tablestore-php-sdk GitHub repository.

Configure access credentials

Create an AccessKey for your Alibaba Cloud account or RAM user, then configure it as an environment variable to avoid hard-coding credentials.

Restart your IDE, terminal, other desktop applications, and background services after configuration to load the updated environment variables. For more information about other types of access credentials, see Configure access credentials.

Linux

  1. Append the environment variables to ~/.bashrc:

    echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bashrc
    echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bashrc
  2. Apply the changes:

    source ~/.bashrc
  3. Verify the environment variables:

    echo $TABLESTORE_ACCESS_KEY_ID
    echo $TABLESTORE_ACCESS_KEY_SECRET

macOS

  1. Check your default shell:

    echo $SHELL
  2. Configure based on your shell type:

    Zsh
    1. Append the environment variables to ~/.zshrc:

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.zshrc
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.zshrc
    2. Apply the changes:

      source ~/.zshrc
    3. Verify the environment variables:

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET
    Bash
    1. Append the environment variables to ~/.bash_profile:

      echo "export TABLESTORE_ACCESS_KEY_ID='YOUR_ACCESS_KEY_ID'" >> ~/.bash_profile
      echo "export TABLESTORE_ACCESS_KEY_SECRET='YOUR_ACCESS_KEY_SECRET'" >> ~/.bash_profile
    2. Apply the changes:

      source ~/.bash_profile
    3. Verify the environment variables:

      echo $TABLESTORE_ACCESS_KEY_ID
      echo $TABLESTORE_ACCESS_KEY_SECRET

Windows

CMD
  1. Set the environment variables in CMD:

    setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID"
    setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET"
  2. Restart CMD and verify:

    echo %TABLESTORE_ACCESS_KEY_ID%
    echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
  1. Run in PowerShell:

    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::SetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)
  2. Verify the environment variables:

    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_ID", [EnvironmentVariableTarget]::User)
    [Environment]::GetEnvironmentVariable("TABLESTORE_ACCESS_KEY_SECRET", [EnvironmentVariableTarget]::User)

Initialize a client

Initialize a Tablestore client and list tables to verify the connection.

Important

Public network access is disabled by default for new instances. To access instance resources over the public network, enable public access in the Network Management of the instance.

require_once __DIR__ . '/vendor/autoload.php';

use Aliyun\OTS\OTSClient as OTSClient;

// Replace yourInstanceName with the actual instance name.
$instanceName = "yourInstanceName";
// Replace yourEndpoint with the actual instance endpoint.
$endpoint = "yourEndpoint";
// Read the AccessKey ID and AccessKey secret from environment variables.
$accessKeyId = getenv('TABLESTORE_ACCESS_KEY_ID');
$accessKeySecret = getenv('TABLESTORE_ACCESS_KEY_SECRET');

// Initialize the Tablestore client.
$client = new OTSClient(array(
    'EndPoint' => $endpoint,
    'AccessKeyID' => $accessKeyId,
    'AccessKeySecret' => $accessKeySecret,
    'InstanceName' => $instanceName,
));

// List all tables in the instance and print the result to the console.
$response = $client->listTable (array ());
print json_encode ($response);

Version compatibility

The latest version is 5.x.x. Compatibility:

  • Compatible with SDK 4.x.x.

  • Not compatible with SDK 2.x.x.

For SDK version history, see PHP SDK historical versions.

FAQ

References

For information about error handling in Tablestore, see Error handling.