kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

API购买CVM

CVM 添加辅助网卡并绑定多 IP

https://cloud.tencent.com/document/product/1199/44153

1
2
3
4
5
6
7
8
9
10
11
12
$ vim /etc/network/interfaces   # 增加如下

auto eth1
iface eth1 inet static
address   172.19.0.13
netmask 172.19.15.255
gateway 172.19.0.1

$ ifconfig eth1 172.19.0.13/20

$ find /proc/sys/net/ -name rp_filter -exec sh -c "echo 0 > {} " \;
$ find /proc/sys/net/ -name rp_filter -exec cat {} \;

python

代理设置 export http_proxy=http://

export https_proxy=http://

SDK

手动安装

https://github.com/TencentCloud/tencentcloud-sdk-python

git clone https://github.com/TencentCloud/tencentcloud-sdk-python.git

python setup.py install

pip 安装

sudo apt-get install python-pip

pip install tencentcloud-sdk-python

请注意,如果同时有 python2 和 python3 环境, python3 环境需要使用 pip3 命令安装。

密钥

https://console.cloud.tencent.com/cam/capi

export TENCENTCLOUD_SECRET_ID=xx

export TENCENTCLOUD_SECRET_KEY=xx

v2.0

方便被其他程序调用,例如网页实现购买

cred = credential.Credential(“your_id”, “your_key”) 中替换自己的ID,KEY

用法: python CVM.py hk 1 会先查寻,如果个数>=n就不购买

查看结果: cat show.log API返回时一般还没分配IP,多调几次 python CVM.py hk 0 就有结果了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# -*- coding: utf-8 -*-
import os
import time
import logging
import sys

handler = logging.FileHandler("all.log") #, encoding='utf-8')
handler.setFormatter(logging.Formatter(fmt='%(asctime)s %(process)d %(levelname)s %(message)s'))
log = logging.getLogger("QQ")
log.addHandler(handler)
log.setLevel(logging.INFO)

handler = logging.FileHandler("show.log", mode='w')
handler.setFormatter(logging.Formatter(fmt='%(asctime)s %(message)s'))
log2 = logging.getLogger("QQQ")
log2.addHandler(handler)
log2.setLevel(logging.INFO)

RZ = {
        "hk" : {
            "Region" : "ap-hongkong",
            "Zone" : ["ap-hongkong-2", "ap-hongkong-1"],
            "ImageId" : "img-3tdtc58k",
            "InstanceType" : ["S2.SMALL1", ],
            "ActionTime" : [1, 2, 3]
            },
        "gz" : {
            "Region" : "ap-guangzhou",
            "Zone" : ["ap-guangzhou-3", "ap-guangzhou-4"],
            "ImageId" : "img-822xs3s2",
            "InstanceType" : ["S2.SMALL1", ],
            "ActionTime" : [1, 2, 3]
            }
        }

from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.cvm.v20170312 import cvm_client, models

# 导入可选配置类
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile

def del_ins(client, ins):
    req = models.TerminateInstancesRequest()

    params = '''{
        "InstanceIds" : [
            "%s"
        ]
    }''' % (ins)
    req.from_json_string(params)

    resp = client.TerminateInstances(req)
    #print(resp.to_json_string(indent=2))
    log.error("%s" % req)
    log.error("%s" % resp)


def buy_ins(client, rz):
    req = models.RunInstancesRequest()
    endtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()+3600*RZ[rz]["ActionTime"][0]-100))

    params = '''{
        "Placement" : {
            "Zone" : "%s"
        },
        "ImageId" : "%s",
        "InstanceType" : "%s",
        "InstanceName" : "%s",
        "InstanceChargeType" : "POSTPAID_BY_HOUR",
        "InternetAccessible" : {
            "InternetChargeType" : "TRAFFIC_POSTPAID_BY_HOUR",
            "InternetMaxBandwidthOut" : 10
        },
        "LoginSettings" : {
            "Password" : "Dis@init3"
        },
        "EnhancedService" : {
            "SecurityService" : {
                "Enabled" : false
            },
            "MonitorService" : {
                "Enabled" : false
            }
        },
        "ActionTimer" : {
            "Externals" : {
            },
            "TimerAction" : "TerminateInstances",
            "ActionTime" : "%s"
        }
    }''' % (RZ[rz]["Zone"][0], RZ[rz]["ImageId"], RZ[rz]["InstanceType"][0], endtime, endtime)

    req.from_json_string(params)

    resp = client.RunInstances(req)
    #print(resp.to_json_string(indent=2))
    log.warning("%s" % req)
    log.warning("%s" % resp)


    count = len(resp.InstanceIdSet)
    log2.info("BUY %s count=%d" % (req.Placement.Zone, count))
    return count


def show_ins(client, rz):
    req = models.DescribeInstancesRequest()

    respFilter = models.Filter()
    respFilter.Name = "zone"
    respFilter.Values = RZ[rz]["Zone"]
    req.Filters = [respFilter]

    resp = client.DescribeInstances(req)
    #print(resp.to_json_string(indent=2))
    log.info("%s" % req)
    log.info("%s" % resp)

    for ins in resp.InstanceSet:
        if ins.PublicIpAddresses and len(ins.PublicIpAddresses) > 0:
            log2.info("%s </br>%s %sMb/s %s </br></br>" % (ins.PublicIpAddresses[0], ins.InstanceName, ins.InternetAccessible.InternetMaxBandwidthOut, ins.InstanceId))
        else:
            log2.info("%s </br>%s %sMb/s %s </br></br>" % ("null", ins.InstanceName, ins.InternetAccessible.InternetMaxBandwidthOut, ins.InstanceId))
    log2.info("SHOW zone=%s count=%d </br>" % (RZ[rz]["Zone"], resp.TotalCount))
    return resp.TotalCount

try:
    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    #cred = credential.Credential(os.environ.get("TENCENTCLOUD_SECRET_ID"), os.environ.get("TENCENTCLOUD_SECRET_KEY"))
    # TODO
    cred = credential.Credential("your_id", "your_key")

    # 实例化一个http选项,可选的,没有特殊需求可以跳过。
    httpProfile = HttpProfile()
    httpProfile.reqMethod = "GET"  # post请求(默认为post请求)
    httpProfile.reqTimeout = 30    # 请求超时时间,单位为秒(默认60秒)
    httpProfile.endpoint = "cvm.ap-guangzhou.tencentcloudapi.com"  # 指定接入地域域名(默认就近接入)

    # 实例化一个client选项,可选的,没有特殊需求可以跳过。
    clientProfile = ClientProfile()
    clientProfile.signMethod = "TC3-HMAC-SHA256"  # 指定签名算法
    clientProfile.language = "en-US"
    clientProfile.httpProfile = httpProfile

    rz = "hk"
    count = 0
    if len(sys.argv) >= 2:
        rz = sys.argv[1]

    if len(sys.argv) >= 3:
        count = int(sys.argv[2])

    if rz not in RZ:
        quit()

    # 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的。
    client = cvm_client.CvmClient(cred, RZ[rz]["Region"], clientProfile)

    if show_ins(client, rz) < count:
        if buy_ins(client, rz) > 0:
            show_ins(client, rz)

    #del_ins(client, "")

except TencentCloudSDKException as err:
    print(err)

v1.0

查询

来自SDK样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# -*- coding: utf-8 -*-
import os

from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.cvm.v20170312 import cvm_client, models

# 导入可选配置类
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
try:
    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    cred = credential.Credential(
        os.environ.get("TENCENTCLOUD_SECRET_ID"),
        os.environ.get("TENCENTCLOUD_SECRET_KEY"))

    # 实例化一个http选项,可选的,没有特殊需求可以跳过。
    httpProfile = HttpProfile()
    httpProfile.reqMethod = "GET"  # post请求(默认为post请求)
    httpProfile.reqTimeout = 30    # 请求超时时间,单位为秒(默认60秒)
    httpProfile.endpoint = "cvm.ap-guangzhou.tencentcloudapi.com"  # 指定接入地域域名(默认就近接入)

    # 实例化一个client选项,可选的,没有特殊需求可以跳过。
    clientProfile = ClientProfile()
    clientProfile.signMethod = "TC3-HMAC-SHA256"  # 指定签名算法
    clientProfile.language = "en-US"
    clientProfile.httpProfile = httpProfile

    # 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的。
    client = cvm_client.CvmClient(cred, "ap-hongkong", clientProfile)

    # 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。
    req = models.DescribeInstancesRequest()

    # 填充请求参数,这里request对象的成员变量即对应接口的入参。
    # 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义。
    respFilter = models.Filter()  # 创建Filter对象, 以zone的维度来查询cvm实例。
    respFilter.Name = "zone"
    respFilter.Values = ["ap-hongkong-1", "ap-hongkong-2"]
    req.Filters = [respFilter]  # Filters 是成员为Filter对象的列表

    # 这里还支持以标准json格式的string来赋值请求参数的方式。下面的代码跟上面的参数赋值是等效的。
    params = '''{
        "Filters": [
            {
                "Name": "zone",
                "Values": ["ap-hongkong-1", "ap-hongkong-2"]
            }
        ]
    }'''
    req.from_json_string(params)

    # 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的。
    # 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应。
    resp = client.DescribeInstances(req)

    # 输出json格式的字符串回包
    print(resp.to_json_string(indent=2))

except TencentCloudSDKException as err:
    print(err)

购买 香港-1core-1GB-5Mbps-1小时后销毁

ImageId 换个公共的 或 自己制作一个

https://console.cloud.tencent.com/cvm/image?rid=5&imageType=PUBLIC_IMAGE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
import os
import time

from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.cvm.v20170312 import cvm_client, models

# 导入可选配置类
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
try:
    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    cred = credential.Credential(
        os.environ.get("TENCENTCLOUD_SECRET_ID"),
        os.environ.get("TENCENTCLOUD_SECRET_KEY"))

    # 实例化一个http选项,可选的,没有特殊需求可以跳过。
    httpProfile = HttpProfile()
    httpProfile.reqMethod = "GET"  # post请求(默认为post请求)
    httpProfile.reqTimeout = 30    # 请求超时时间,单位为秒(默认60秒)
    httpProfile.endpoint = "cvm.ap-guangzhou.tencentcloudapi.com"  # 指定接入地域域名(默认就近接入)

    # 实例化一个client选项,可选的,没有特殊需求可以跳过。
    clientProfile = ClientProfile()
    clientProfile.signMethod = "TC3-HMAC-SHA256"  # 指定签名算法
    clientProfile.language = "en-US"
    clientProfile.httpProfile = httpProfile

    # 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的。
    client = cvm_client.CvmClient(cred, "ap-hongkong", clientProfile)

    # 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。
    req = models.RunInstancesRequest()

    # 填充请求参数,这里request对象的成员变量即对应接口的入参。
    # 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义。

    # 这里还支持以标准json格式的string来赋值请求参数的方式。下面的代码跟上面的参数赋值是等效的。
    params = '''{
        "Region" : "ap-hongkong",
        "Placement" : {
            "Zone" : "ap-hongkong-2"
        },
        "ImageId" : "img-7b63u5v2",
        "InstanceChargeType" : "POSTPAID_BY_HOUR",
        "InstanceType" : "S2.SMALL1",
        "InternetAccessible" : {
            "InternetChargeType" : "TRAFFIC_POSTPAID_BY_HOUR",
            "InternetMaxBandwidthOut" : 5
        },
        "LoginSettings" : {
            "Password" : "QAZwsx123"
        },
        "EnhancedService" : {
            "SecurityService" : {
                "Enabled" : false
            },
            "MonitorService" : {
                "Enabled" : false
            }
        },
        "ActionTimer" : {
            "Externals" : {
            },
            "TimerAction" : "TerminateInstances",
            "ActionTime" : "%s"
        }
    }''' % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()+3300)))

    req.from_json_string(params)

    #print(req)

    # 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的。
    # 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应。
    resp = client.RunInstances(req)

    # 输出json格式的字符串回包
    print(resp.to_json_string(indent=2))

except TencentCloudSDKException as err:
    print(err)

退还CVM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*-
import os

from tencentcloud.common import credential
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
# 导入对应产品模块的client models。
from tencentcloud.cvm.v20170312 import cvm_client, models

# 导入可选配置类
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
try:
    # 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
    cred = credential.Credential(
        os.environ.get("TENCENTCLOUD_SECRET_ID"),
        os.environ.get("TENCENTCLOUD_SECRET_KEY"))

    # 实例化一个http选项,可选的,没有特殊需求可以跳过。
    httpProfile = HttpProfile()
    httpProfile.reqMethod = "GET"  # post请求(默认为post请求)
    httpProfile.reqTimeout = 30    # 请求超时时间,单位为秒(默认60秒)
    httpProfile.endpoint = "cvm.ap-guangzhou.tencentcloudapi.com"  # 指定接入地域域名(默认就近接入)

    # 实例化一个client选项,可选的,没有特殊需求可以跳过。
    clientProfile = ClientProfile()
    clientProfile.signMethod = "TC3-HMAC-SHA256"  # 指定签名算法
    clientProfile.language = "en-US"
    clientProfile.httpProfile = httpProfile

    # 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的。
    client = cvm_client.CvmClient(cred, "ap-hongkong", clientProfile)

    # 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。
    req = models.TerminateInstancesRequest()

    # 填充请求参数,这里request对象的成员变量即对应接口的入参。
    params = '''{
        "InstanceIds" : [
            "ins-6y6yfpdw"
        ]
    }'''
    req.from_json_string(params)

    #print(req)
    # 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的。
    # 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应。
    resp = client.TerminateInstances(req)

    # 输出json格式的字符串回包
    print(resp.to_json_string(indent=2))

except TencentCloudSDKException as err:
    print(err)

phpSDK 已经更新,以下需要改

php

看 vendor/GuzzleHttp/Client.php 中 configureDefaults ,代理需要设置

export HTTP_PROXY=tcp://xx

export HTTPS_PROXY=tcp://xx

或者安装 php-curl 就可以改用

export http_proxy=http://

export https_proxy=http://

SDK

https://github.com/TencentCloud/tencentcloud-sdk-php

git clone https://github.com/TencentCloud/tencentcloud-sdk-php.git

密钥

https://console.cloud.tencent.com/cam/capi

export TENCENTCLOUD_SECRET_ID=xx

export TENCENTCLOUD_SECRET_KEY=xx

查询

来自SDK样例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
require_once '../../../TCloudAutoLoader.php';
// 导入对应产品模块的client
use TencentCloud\Cvm\V20170312\CvmClient;
// 导入要请求接口对应的Request类
use TencentCloud\Cvm\V20170312\Models\DescribeInstancesRequest;
use TencentCloud\Cvm\V20170312\Models\Filter;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
// 导入可选配置类
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;

try {
    // 实例化一个证书对象,入参需要传入腾讯云账户secretId,secretKey
    //$cred = new Credential("secretId", "secretKey");
    $cred = new Credential(getenv("TENCENTCLOUD_SECRET_ID"), getenv("TENCENTCLOUD_SECRET_KEY"));

    // 实例化一个http选项,可选的,没有特殊需求可以跳过
    $httpProfile = new HttpProfile();
    $httpProfile->setReqMethod("GET");  // post请求(默认为post请求)
    $httpProfile->setReqTimeout(30);    // 请求超时时间,单位为秒(默认60秒)
    $httpProfile->setEndpoint("cvm.ap-guangzhou.tencentcloudapi.com");  // 指定接入地域域名(默认就近接入)

    // 实例化一个client选项,可选的,没有特殊需求可以跳过
    $clientProfile = new ClientProfile();
    $clientProfile->setSignMethod("TC3-HMAC-SHA256");  // 指定签名算法(默认为HmacSHA256)
    $clientProfile->setHttpProfile($httpProfile);

    // 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的
    $client = new CvmClient($cred, "ap-hongkong", $clientProfile);

    // 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。
    $req = new DescribeInstancesRequest();

    // 填充请求参数,这里request对象的成员变量即对应接口的入参
    // 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
    $respFilter = new Filter();  // 创建Filter对象, 以zone的维度来查询cvm实例
    $respFilter->Name = "zone";
    $respFilter->Values = ["ap-hongkong-1", "ap-hongkong-2"];
    $req->Filters = [$respFilter];  // Filters 是成员为Filter对象的列表

    // 这里还支持以标准json格式的string来赋值请求参数的方式。下面的代码跟上面的参数赋值是等效的
    $params = [
        "Filters" => [
            [
                "Name" => "zone",
                "Values" => ["ap-hongkong-1", "ap-hongkong-2"]
            ]
        ]
    ];
    $req->fromJsonString(json_encode($params));

    // 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的
    // 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应
    $resp = $client->DescribeInstances($req);

    // 输出json格式的字符串回包
    print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
    echo $e;
}

购买 香港-1core-1GB-5Mbps-1小时后销毁

ImageId 换个公共的 或 自己制作一个

https://console.cloud.tencent.com/cvm/image?rid=5&imageType=PUBLIC_IMAGE

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require_once '../../../TCloudAutoLoader.php';
// 导入对应产品模块的client
use TencentCloud\Cvm\V20170312\CvmClient;
// 导入要请求接口对应的Request类
use TencentCloud\Cvm\V20170312\Models\RunInstancesRequest;
use TencentCloud\Cvm\V20170312\Models\Filter;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
// 导入可选配置类
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;

try {
    // 实例化一个证书对象,入参需要传入腾讯云账户secretId,secretKey
    //$cred = new Credential("secretId", "secretKey");
    $cred = new Credential(getenv("TENCENTCLOUD_SECRET_ID"), getenv("TENCENTCLOUD_SECRET_KEY"));

    // 实例化一个http选项,可选的,没有特殊需求可以跳过
    $httpProfile = new HttpProfile();
    $httpProfile->setReqMethod("GET");  // post请求(默认为post请求)
    $httpProfile->setReqTimeout(30);    // 请求超时时间,单位为秒(默认60秒)
    $httpProfile->setEndpoint("cvm.ap-guangzhou.tencentcloudapi.com");  // 指定接入地域域名(默认就近接入)

    // 实例化一个client选项,可选的,没有特殊需求可以跳过
    $clientProfile = new ClientProfile();
    $clientProfile->setSignMethod("TC3-HMAC-SHA256");  // 指定签名算法(默认为HmacSHA256)
    $clientProfile->setHttpProfile($httpProfile);

    // 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的
    $client = new CvmClient($cred, "ap-hongkong", $clientProfile);

    // 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。
    $req = new RunInstancesRequest();

    // 填充请求参数,这里request对象的成员变量即对应接口的入参
    // 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
    // 这里还支持以标准json格式的string来赋值请求参数的方式。下面的代码跟上面的参数赋值是等效的
    $params = [
        "Region" => "ap-hongkong",
        "Placement" => [
            "Zone" => "ap-hongkong-2"
        ],
        "ImageId" => "img-7b63u5v2",
        "InstanceChargeType" => "POSTPAID_BY_HOUR",
        "InstanceType" => "S2.SMALL1",
        "InternetAccessible" => [
            "InternetChargeType" => "TRAFFIC_POSTPAID_BY_HOUR",
            "InternetMaxBandwidthOut" => 5
        ],
        "LoginSettings" => [
            "Password" => "QAZwsx123"
        ],
        "EnhancedService" => [
            "SecurityService" => [
                "Enabled" => false
            ],
            "MonitorService" => [
                "Enabled" => false
            ]
        ],
        "ActionTimer" => [
            "Externals" => [
            ],
            "TimerAction" => "TerminateInstances",
            "ActionTime" => date('Y-m-d H:i:s', time()+3300),
        ]
    ];
    $req->fromJsonString(json_encode($params));

    #var_dump($req);
    // 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的
    // 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应
    $resp = $client->RunInstances($req);

    // 输出json格式的字符串回包
    print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
    echo $e;
}

退还CVM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
require_once '../../../TCloudAutoLoader.php';
// 导入对应产品模块的client
use TencentCloud\Cvm\V20170312\CvmClient;
// 导入要请求接口对应的Request类
use TencentCloud\Cvm\V20170312\Models\TerminateInstancesRequest;
use TencentCloud\Cvm\V20170312\Models\Filter;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
// 导入可选配置类
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;

try {
    // 实例化一个证书对象,入参需要传入腾讯云账户secretId,secretKey
    //$cred = new Credential("secretId", "secretKey");
    $cred = new Credential(getenv("TENCENTCLOUD_SECRET_ID"), getenv("TENCENTCLOUD_SECRET_KEY"));

    // 实例化一个http选项,可选的,没有特殊需求可以跳过
    $httpProfile = new HttpProfile();
    $httpProfile->setReqMethod("GET");  // post请求(默认为post请求)
    $httpProfile->setReqTimeout(30);    // 请求超时时间,单位为秒(默认60秒)
    $httpProfile->setEndpoint("cvm.ap-guangzhou.tencentcloudapi.com");  // 指定接入地域域名(默认就近接入)

    // 实例化一个client选项,可选的,没有特殊需求可以跳过
    $clientProfile = new ClientProfile();
    $clientProfile->setSignMethod("TC3-HMAC-SHA256");  // 指定签名算法(默认为HmacSHA256)
    $clientProfile->setHttpProfile($httpProfile);

    // 实例化要请求产品(以cvm为例)的client对象,clientProfile是可选的
    $client = new CvmClient($cred, "ap-hongkong", $clientProfile);

    // 实例化一个cvm实例信息查询请求对象,每个接口都会对应一个request对象。
    $req = new TerminateInstancesRequest();

    // 填充请求参数,这里request对象的成员变量即对应接口的入参
    // 你可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
    // 这里还支持以标准json格式的string来赋值请求参数的方式。下面的代码跟上面的参数赋值是等效的
    $params = [
        "InstanceIds" => [
            "ins-1rfi3vms"
        ]
    ];
    $req->fromJsonString(json_encode($params));

    // 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的
    // 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应
    $resp = $client->TerminateInstances($req);

    // 输出json格式的字符串回包
    print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
    echo $e;
}