PHP SDK Usage Example
This section uses the DSC AddBuckets API, which adds asset authorization, 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 a dependency management tool for PHP. It allows you to declare and install dependencies in your project.
Run the commands below in the CLI to install Composer and the Huawei Cloud PHP SDK. PHP SDK 3.1.203 is used as an example. You can obtain the latest SDK version from the 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 of using the Huawei Cloud PHP SDK to call the AddBuckets 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 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
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\Dsc\V1\DscClient;
use HuaweiCloud\SDK\Dsc\Model\AddBucketsRequest;
use HuaweiCloud\SDK\Dsc\Model\AddBucketsRequestBody;
use HuaweiCloud\SDK\Dsc\Model\ServerId;
use HuaweiCloud\SDK\Dsc\Model\AddBucketsOption;
// 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://sdg.af-south-1.myhuaweicloud.com";
$projectId = "";
$credentials = new BasicCredentials($ak,$sk,$projectId);
$config = HttpConfig::getDefaultConfig();
$config->setIgnoreSslVerification(true);
$client = DscClient::newBuilder(new DscClient)
->withHttpConfig($config)
->withEndpoint($endpoint)
->withCredentials($credentials)
->build();
$request = new AddBucketsRequest();
$body = new BucketsRequest();
$request->setBody($body);
try {
$response = $client->AddBuckets($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