Use the Open Search PHP SDK to retrieve all applications under your Alibaba Cloud account.
Prerequisites
Before you begin, ensure that you have:
An Alibaba Cloud account with Open Search applications created
The PHP SDK installed. For installation instructions, see the SDK documentation.
A Resource Access Management (RAM) user with the
AliyunServiceRoleForOpenSearchrole granted by your Alibaba Cloud account. See AliyunServiceRoleForOpenSearch and Access authorization rules.
Use a RAM user instead of your Alibaba Cloud root account to call API operations. The root account AccessKey pair has unrestricted access to all API operations. Never embed an AccessKey pair directly in code. To create a RAM user, see Create a RAM user. To create an AccessKey pair, see Create an AccessKey pair.
Set environment variables
Set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to the AccessKey ID and AccessKey secret of your RAM user.
Linux and macOS
export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id>
export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret>Windows
Create an environment variable file and add
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETwith their respective values.Restart Windows for the changes to take effect.
Create a configuration file
Create Config.inc.php with the client configuration. This file initializes the OpenSearchClient object used by all subsequent SDK operations.
<?php
// Import the autoloader.
require_once("../OpenSearch/Autoloader/Autoloader.php");
use OpenSearch\Client\OpenSearchClient;
// Read credentials from environment variables.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$secret = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
// Replace with your region endpoint.
// Find the endpoint on the Basic Information page of your application in the console.
$endPoint = '<region endPoint>';
// Replace with your application name.
$appName = '<app name>';
// Replace with your drop-down suggestion model name.
$suggestName = '<suggest name>';
// Enable debug mode to print trace information.
$options = array('debug' => true);
// Create the client.
$client = new OpenSearchClient($accessKeyId, $secret, $endPoint, $options);Replace the following placeholders:
| Placeholder | Description |
|---|---|
<region endPoint> | The API endpoint for your region. Find it on the Basic Information page of your application in the console. |
<app name> | The name of your Open Search application. |
<suggest name> | The name of your drop-down suggestion model. |
Query the application list
The following code queries all applications in your account using a paginated request. It uses AppClient.listAll() with a Pageable object to control the result page and size.
<?php
// Import the configuration file.
require_once("Config.inc.php");
use OpenSearch\Client\AppClient;
use OpenSearch\Generated\Common\Pageable;
// Set pagination: retrieve the first page with up to 10 results.
$pageable = new Pageable(array('page' => 1, 'size' => 10));
// Create an AppClient using the shared client from Config.inc.php.
$appClient = new AppClient($client);
// Retrieve and print the application list.
$ret = $appClient->listAll($pageable);
print_r($ret);
// Print trace information for debugging.
echo $ret->traceInfo->tracer;Pagination parameters
| Parameter | Type | Description |
|---|---|---|
page | integer | The page number to retrieve. Starts at 1. |
size | integer | The number of applications to return per page. |
To retrieve the next page, increment page by 1 and call listAll() again.
Response fields
The $ret object returned by listAll() includes the following fields:
| Field | Description |
|---|---|
result | The list of applications for the requested page. |
traceInfo->tracer | Debug trace information for the request. |