Help Center/ Cloud Search Service/ SDK Reference/ PHP SDK Calling Example
Updated on 2026-11-17 GMT+08:00

PHP SDK Calling Example

This section uses the CSS API for querying the cluster list (ListClustersDetails) as an example to describe how to use the Huawei Cloud PHP SDK.

Prerequisites

  • You have obtained the access key (AK) and secret access key (SK) corresponding to your Huawei Cloud account. For details, see Creating Access Keys.
  • PHP 5.6 or later has been installed in the environment where the SDK runs.

Installing the SDK

You are advised to use Composer to install the Huawei Cloud PHP SDK. Composer is the dependency management tool for PHP. It allows you to declare and install dependencies in a project.

Run the commands below in the CLI to install Composer and Huawei Cloud PHP SDK. The PHP SDK 3.1.203 is used as an example. You can obtain the latest SDK version from SDK Center.

# Install Composer. If Composer has been installed, skip this step.
curl -sS https://getcomposer.org/installer | php 
 
# Install the PHP SDK.
composer require huaweicloud/huaweicloud-sdk-php:3.1.203

Using the SDK

The following is a complete PHP example, which shows how to use the Huawei Cloud PHP SDK to call the ListClustersDetails API.

  • To obtain the SDK example code for other APIs, see Obtaining SDK Sample Code.
  • This example provides only the basic API parameter settings. For more advanced settings, see the User Manual of the SDK.
  • There will be huge security risks if the AK and SK used for authentication are directly written into the code. You are advised to encrypt the AK and SK and store them in the configuration file or environment variables, and decrypt them when you use them. In this example, the AK and SK are stored as the environment variables CLOUD_SDK_AK and CLOUD_SDK_SK. For details, see Environment Variables.
<?php
require_once "vendor/autoload.php";
use HuaweiCloud\SDK\Core\Auth\BasicCredentials;
use HuaweiCloud\SDK\Core\Http\HttpConfig;
use HuaweiCloud\SDK\Core\Exceptions\ConnectionException;
use HuaweiCloud\SDK\Core\Exceptions\RequestTimeoutException;
use HuaweiCloud\SDK\Core\Exceptions\ServiceResponseException;
use HuaweiCloud\SDK\Css\V1\CssClient;
use HuaweiCloud\SDK\Css\V1\Model\ListClustersDetailsRequest;

// The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
// In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
$ak = getenv('CLOUD_SDK_AK');
$sk = getenv('CLOUD_SDK_SK');
$endpoint = "https://css.cn-north-4.myhuaweicloud.com";
$projectId = "";
$credentials = new BasicCredentials($ak,$sk,$projectId);
$config = HttpConfig::getDefaultConfig();
$config->setIgnoreSslVerification(true);

$client = CssClient::newBuilder(new CssClient)
  ->withHttpConfig($config)
  ->withEndpoint($endpoint)
  ->withCredentials($credentials)
  ->build();
$request = new ListClustersDetailsRequest();

try {
  $response = $client->ListClustersDetails($request);
  echo "\n";
  echo $response;
} catch (ConnectionException $e) {
  $msg = $e->getMessage();
  echo "\n". $msg ."\n";
} catch (RequestTimeoutException $e) {
  $msg = $e->getMessage();
  echo "\n". $msg ."\n";
} catch (ServiceResponseException $e) {
  echo "\n";
  echo $e->getHttpStatusCode(). "\n";
  echo $e->getRequestId(). "\n";
  echo $e->getErrorCode() . "\n";
  echo $e->getErrorMsg() . "\n";
}