PHP SDK Calling Example
This section uses the API for querying the instance list (ListInstances) 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 following commands in the CLI to install the 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 ListInstances 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 set environment variables, and decrypt them when using 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
namespace HuaweiCloud\SDK\Kafka\V2\Model;
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\Kafka\V2\KafkaClient;
// ==================== 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://dms.ap-southeast-1.myhuaweicloud.com";
$projectId = "195e********4a708ad9f9********1d";
// Configure an authentication credential.
$credentials = new BasicCredentials($ak,$sk,$projectId);
$config = HttpConfig::getDefaultConfig();
$config->setIgnoreSslVerification(true);
// Initialize the client.
$client = KafkaClient::newBuilder(new KafkaClient)
->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 ListInstancesRequest();
$request->setEngine("kafka");
// ==================== 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->ListInstances($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";
} What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot