Help Center/ Data Warehouse Service/ SDK Reference/ PHP SDK Calling Example
Updated on 2026-11-19 GMT+08:00

PHP SDK Calling Example

This section uses the DWS API for querying flavor information (ListNodeTypes) 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. For details about the latest SDK version, visit 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 ListNodeTypes 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\Dws\V2\DwsClient;
use HuaweiCloud\SDK\DWS\V2\Model\ListNodeTypesRequest;

// ==================== Initialize the client. ==================== 
// Obtain the AK/SK from the environment variables. Before running, you need to set the AK/SK as environment variables.
$ak = getenv('CLOUD_SDK_AK');
$sk = getenv('CLOUD_SDK_SK');
$endpoint = "https://dws.af-north-1.myhuaweicloud.com";
$projectId = "8d6c92e4e2d14ae685a697adfe259256";

// Configure an authentication credential.
$credentials = new BasicCredentials($ak,$sk,$projectId);
$config = HttpConfig::getDefaultConfig();
$config->setIgnoreSslVerification(true);

// Initialize the client.
$client = DwsClient::newBuilder(new DwsClient)
  ->withHttpConfig($config)
  ->withEndpoint($endpoint)
  ->withCredentials($credentials)
  ->build();

// ==================== Construct a request object. ====================
// Use the request object provided by the SDK to encapsulate request parameters.
$request = new ListNodeTypesRequest();

// ==================== Initiate a request and handle the response. ====================
// Use the client to call the corresponding API. If the API is successfully called, a response object is returned. If the API fails to be called, an exception is thrown.
try {
  $response = $client->ListNodeTypes($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";
}