CommonClient.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. require_once __DIR__.'/../../vendor/autoload.php';
  3. use TencentCloud\Common\CommonClient;
  4. use TencentCloud\Common\Exception\TencentCloudSDKException;
  5. use TencentCloud\Common\Credential;
  6. try {
  7. $cred = new Credential(
  8. getenv("TENCENTCLOUD_SECRET_ID"),
  9. getenv("TENCENTCLOUD_SECRET_KEY")
  10. );
  11. // 和各产品 Client 相比,CommonClient 需要先指定产品名和版本号
  12. $client = new CommonClient("cvm", "2017-03-12", $cred, "ap-guangzhou");
  13. $headers = array();
  14. $body = [
  15. "Filters" => [
  16. [
  17. "Name" => "zone",
  18. "Values" => ["ap-guangzhou-1", "ap-guangzhou-2"]
  19. ]
  20. ]
  21. ];
  22. // 返回的是 array 对象,如请求失败则抛出异常
  23. $resp = $client->callJson(
  24. // 接口名
  25. "DescribeInstances",
  26. // 请求体,目前必须为 array 对象
  27. $body,
  28. // 请求头,可留空
  29. //$headers,
  30. );
  31. echo $resp["TotalCount"].PHP_EOL;
  32. }
  33. catch(TencentCloudSDKException $e) {
  34. echo $e;
  35. }