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 -vto check your version.NoteTablestore uses 64-bit integers. 32-bit PHP can only represent them as
stringvalues 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 -mto verify installation.
Install the SDK
Install the SDK with Composer or from a source package.
Composer
To install with Composer:
-
Run
composer require aliyun/aliyun-tablestore-sdk-phpin the project root, or add the dependency to composer.json.NoteIf you encounter network errors with Composer, use the China region mirror. Run
composer config -g repo.packagist composer https://developer.aliyun.com/composerin the command line.{ "require": { "aliyun/aliyun-tablestore-sdk-php": "^5.1" } } -
Run
composer install. The resulting directory structure:. ├── app.php ├── composer.json ├── composer.lock └── vendorvendor/contains the installed libraries. Include the autoloader in your application:require_once __DIR__ . '/vendor/autoload.php';NoteSkip this step if your project already includes autoload.php.
Source package
Download the source package:
-
Go to GitHub and download the version you need.
-
Download the SDK source package.
The SDK includes sample programs for common wide column model operations:
-
Download and extract the SDK package. Samples are in the
examplesdirectory. -
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
-
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 -
Apply the changes:
source ~/.bashrc -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
macOS
-
Check your default shell:
echo $SHELL -
Configure based on your shell type:
Zsh
-
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 -
Apply the changes:
source ~/.zshrc -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
Bash
-
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 -
Apply the changes:
source ~/.bash_profile -
Verify the environment variables:
echo $TABLESTORE_ACCESS_KEY_ID echo $TABLESTORE_ACCESS_KEY_SECRET
-
Windows
CMD
-
Set the environment variables in CMD:
setx TABLESTORE_ACCESS_KEY_ID "YOUR_ACCESS_KEY_ID" setx TABLESTORE_ACCESS_KEY_SECRET "YOUR_ACCESS_KEY_SECRET" -
Restart CMD and verify:
echo %TABLESTORE_ACCESS_KEY_ID% echo %TABLESTORE_ACCESS_KEY_SECRET%
PowerShell
-
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) -
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.
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.