PHP code example of weforks / tencentcloud-sdk-php

1. Go to this page and download the library: Download weforks/tencentcloud-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/ */

    

weforks / tencentcloud-sdk-php example snippets



TencentCloud\Cvm\V20170312\CvmClient;
use TencentCloud\Cvm\V20170312\Models\DescribeInstancesRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;

try {
    // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中。
    // 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
    // $cred = new Credential("SecretId", "SecretKey");
    $cred = new Credential(getenv("TENCENTCLOUD_SECRET_ID"),
                           getenv("TENCENTCLOUD_SECRET_KEY"));
    $client = new CvmClient($cred, "ap-guangzhou");
    $req = new DescribeInstancesRequest();
    $resp = $client->DescribeInstances($req);
    print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
    echo $e;
}

$httpProfile = new HttpProfile();
$httpProfile->setProxy('https://ip:port');

$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);

$client = new OcrClient($cred, 'ap-beijing', $this->clientProfile);

use TencentCloud\Common\Profile\RegionBreakerProfile;
// 开启地域容灾
$clientProfile->enableRegionBreaker=true;  
// 设置主备节点,以cvm产品为例,假设主节点在上海,备节点在北京,则配置如下
$regionBreakerProfile = new RegionBreakerProfile(
    "cvm.ap-shanghai.tencentcloudapi.com",	// 主节点
    "cvm.ap-beijing.tencentcloudapi.com"    // 备节点	
);
$clientProfile->setRegionBreakerProfile($regionBreakerProfile);