PHP code example of huaweicloud / huaweicloud-sdk-php
1. Go to this page and download the library: Download huaweicloud/huaweicloud-sdk-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
huaweicloud / huaweicloud-sdk-php example snippets
php
iCloud\SDK\Core\Auth\GlobalCredentials;
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\Iam\V3\IamClient;
use HuaweiCloud\SDK\Iam\V3\Model\ListPermanentAccessKeysRequest;
use Monolog\Logger;
//Do not hard-code authentication information into the code, as this may pose a security risk
$ak = getenv("HUAWEICLOUD_SDK_AK");
$sk = getenv("HUAWEICLOUD_SDK_SK");
$endpoint = "{your endpoint}";
$domainId = "{your domain id}";
$config = HttpConfig::getDefaultConfig();
$config->setIgnoreSslVerification(true);
$credentials = new GlobalCredentials($ak,$sk,$domainId);
$iamClient = IamClient::newBuilder()
->withHttpConfig($config)
->withEndpoint($endpoint)
->withCredentials($credentials)
->withStreamLogger($stream = 'php://stdout',$logLevel =Logger::INFO)
->withFileLogger($logPath='./test_log.txt', $logLevel = Logger::INFO)
->build();
function listPermanentAccessKeys($iamClient)
{
$listPermanentAccessKeysRequest = new ListPermanentAccessKeysRequest(array('userId'=>"{your user id}"));
try {
$response = $iamClient->listPermanentAccessKeys($listPermanentAccessKeysRequest);
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";
}
}
listPermanentAccessKeys($iamClient);
php
// Use default configuration
$config = HttpConfig::getDefaultConfig();
php
// Skip SSL certification checking while using https protocol if needed
$config->setIgnoreSslVerification(true);
// Server ca certification if needed
$config->setCertFile($yourCertFile);
php
// Regional services
$basicCredentials = new BasicCredentials($ak,$sk,$projectId);
// Global services
$globalCredentials = new GlobalCredentials($ak,$sk,$domainId);
php
// Initialize specified service client instance, take IamClient for example
$iamClient = IamClient::newBuilder()
->withHttpConfig($config)
->withEndpoint($endpoint)
->withCredentials($globalCredentials)
->build();
php
// Initialize a request and print response, take interface of listPermanentAccessKeys for example
$request = new ListPermanentAccessKeysRequest(array(userId=>"{your user id}"));
$response = $iamClient->listPermanentAccessKeys($request);
echo response;