PHP SDK Usage Example
This section describes how to use the Huawei Cloud PHP SDK by calling the CreateInstanceNamespace API of SWR.
Prerequisites
- You have obtained the AK and SK corresponding to your Huawei Cloud account. For details, see Creating Access Keys.
- PHP 5.6 or a later version 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 example demonstrating how to use the Huawei Cloud PHP SDK to call the CreateInstanceNamespace 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 written into the code. You are advised to encrypt the AK and SK and store them in a configuration file or environment variables, and decrypt them when needed to ensure security. 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\Swr\V2\SwrClient; use HuaweiCloud\SDK\Swr\V2\Model\CreateInstanceNamespaceRequest; use HuaweiCloud\SDK\Swr\V2\Model\CreateInstanceNamespaceRequestBody; use HuaweiCloud\SDK\Swr\V2\Model\NamespaceMetadata; // 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://swr-api.cn-north-4.myhuaweicloud.com"; $projectId = "067cf8aecf3845d1ad7a5d308322f13b"; $credentials = new BasicCredentials($ak,$sk,$projectId); $config = HttpConfig::getDefaultConfig(); $config->setIgnoreSslVerification(true); $client = SwrClient::newBuilder(new SwrClient) ->withHttpConfig($config) ->withEndpoint($endpoint) ->withCredentials($credentials) ->build(); $request = new CreateInstanceNamespaceRequest(); $request->setInstanceId("instance_id"); $body = new CreateInstanceNamespaceRequestBody(); $metadatabody = new NamespaceMetadata(); $metadatabody->setPublic("true"); $body->setMetadata($metadatabody); $body->setNamespaceName("test"); $request->setBody($body); try { $response = $client->CreateInstanceNamespace($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