tencent cloud

PHP
Last updated: 2024-12-02 16:29:12
PHP
Last updated: 2024-12-02 16:29:12

Overview

Welcome to Tencent Cloud Software Development Kit (SDK) 3.0, a companion tool for the TencentCloud API 3.0 platform. SDK 3.0 is unified and features the same SDK usage, API call methods, error codes, and returned packet formats for different programming languages.
This document describes how to use, debug, and connect to TencentCloud APIs with the SDK for PHP 3.0 as an example.
This version currently supports various Tencent Cloud products such as CVM, VPC, and CBS and will support more products in the future.

Dependent Environment

PHP 5.6.33 or above.
Get the security credential, which consists of SecretId and SecretKey. SecretId is used to identify the API requester, while SecretKey is a key used for signature string encryption and authentication by the server. You can get them on the API Key Management page as shown below:


Note:
Your security credential represents your account identity and granted permissions, which is equivalent to your login password. Do not disclose it to others.
Get the calling address (endpoint), which is generally in the format of *.tencentcloudapi.com and varies by product. For example, the endpoint of CVM is cvm.tencentcloudapi.com. For specific endpoints, please see the API documentation of the corresponding product .

Installing SDK for PHP 3.0

Installation through Composer is the recommended way to use the SDK for PHP. Composer is a dependency manager for PHP. For more information, please visit Composer official website.
Note:
Composer requires PHP 5.3.2+ and above, and openssl needs to be enabled.

Step 1. Install Composer

For Windows, go to Composer official website to download the installation package and install it.
For Unix, install it by running the following command on the command line:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Step 2. Add a mirror source

If you are in the Chinese mainland, you can use a Tencent Cloud mirror source to speed up the download by running the following command in the opened command window:
composer config -g repos.packagist composer https://mirrors.tencent.com/composer/

Step 3. Add dependencies

In the opened command window, run the command to install the SDK (in the specified location). For example, to install in the C:\\Users\\···> directory, open the command window at the specified location and run the following command:
composer require tencentcloud/tencentcloud-sdk-php

Step 4. Add references

Import the following code. Note: this example is for reference only. Composer will generate a vendor directory in the project root directory, whose actual absolute path is /path/to/ (if you perform this operation in the project root directory, you can omit the absolute path).
require '/path/to/vendor/autoload.php';
Note:
If you only want to install the package of a certain product, you can use composer require tencentcloud/product name, such as composer require tencentcloud/cvm.

Using SDK

We recommend you use API 3.0 Explorer, which provides various capabilities such as online call, signature verification, SDK code generation, and quick API search, greatly improving the ease of use of TencentCloud API 3.0 and SDK.
You can also refer to the examples in the examples directory in the SDK repository for more usage information.
The following uses the instance querying API DescribeInstances as an example:
Simplified
Detailed
<?php
require_once '/path/to/vendor/autoload.php';

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

try {
$cred = new Credential("secretId", "secretKey");
$client = new CvmClient($cred, "ap-guangzhou");
$req = new DescribeInstancesRequest();
$resp = $client->DescribeInstances($req);
print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
echo $e;
}
<?php
require_once '/path/to/vendor/autoload.php';
// Import the client of the corresponding product module
use TencentCloud\\Cvm\\V20170312\\CvmClient;
// Import the `Request` class corresponding to the request API
use TencentCloud\\Cvm\\V20170312\\Models\\DescribeInstancesRequest;
use TencentCloud\\Cvm\\V20170312\\Models\\Filter;
use TencentCloud\\Common\\Exception\\TencentCloudSDKException;
use TencentCloud\\Common\\Credential;
// Import the optional configuration classes
use TencentCloud\\Common\\Profile\\ClientProfile;
use TencentCloud\\Common\\Profile\\HttpProfile;

try {
// Instantiate a certificate object. The Tencent Cloud account `secretId` and `secretKey` need to be passed in as input parameters
$cred = new Credential("secretId", "secretKey");

// (Optional) Instantiate an HTTP option
$httpProfile = new HttpProfile();
// Configure the proxy
// $httpProfile->setProxy("https://ip:port");
$httpProfile->setReqMethod("GET"); // GET request (POST request is used by default)
$httpProfile->setReqTimeout(30); // Specify the request timeout value in seconds. The default value is 60s
$httpProfile->setEndpoint("cvm.ap-shanghai.tencentcloudapi.com"); // Specify the endpoint. If you do not specify the endpoint, nearby access is enabled by default

// Instantiate a client option (optional; skip if no special requirements are present)
$clientProfile = new ClientProfile();
$clientProfile->setSignMethod("TC3-HMAC-SHA256"); // Specify the signature algorithm. The default value is `HmacSHA256`
$clientProfile->setHttpProfile($httpProfile);

// Instantiate the client object of the requested product (with CVM as an example). `clientProfile` is optional
$client = new CvmClient($cred, "ap-shanghai", $clientProfile);

// Instantiate a CVM instance information query request object. Each API corresponds to a request object
$req = new DescribeInstancesRequest();

// Populate the request parameters. Here, the member variables of the request object are the input parameters of the corresponding API
// You can view the definition of the request parameters in the API documentation at the official website or by redirecting to the definition of the request object
$respFilter = new Filter(); // Create a `Filter` object to query CVM instances in the `zone` dimension
$respFilter->Name = "zone";
$respFilter->Values = ["ap-shanghai-1", "ap-shanghai-2"];
$req->Filters = [$respFilter]; // `Filters` is a list of `Filter` objects

// Initialize the request by calling the `DescribeInstances` method on the client object. Note: the request method name corresponds to the request object
// The returned `resp` is an instance of the `DescribeInstancesResponse` class which corresponds to the request object
$resp = $client->DescribeInstances($req);

// A string return packet in JSON format is output
print_r($resp->toJsonString());

// You can also take a single value
// You can view the definition of the return field in the API documentation at the official website or by redirecting to the definition of the response object
print_r($resp->TotalCount);
}
catch(TencentCloudSDKException $e) {
echo $e;
}

More samples

You can find more detailed samples in the examples directory in the GitHub repository.

Relevant Configuration

Proxy

If there is a proxy in your environment, you need to set the system environment variable https_proxy; otherwise, it may not be called normally, and a connection timeout exception will be thrown. You can also use GuzzleHttp to proxy the configuration:
$cred = new Credential("secretId", "secretKey");

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

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

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

FAQs

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback