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.
*.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 .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.
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
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/
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
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 ascomposer require tencentcloud/cvm
.
The following uses the instance querying API DescribeInstances
as an example:
<?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;
}
You can find more detailed samples in the examples
directory in the GitHub repository.
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);
If there is a problem with your PHP environment certificate, errors similar to cURL error 60: See http://curl.haxx.se/libcurl/c/libcurl-errors.html
may occur, which can be solved as follows:
cacert.pem
at https://curl.haxx.se/ca/cacert.pem and save it to the PHP installation path.php.ini
file: delete the semicolon comment (;) before the curl.cainfo
configuration item and set the value to the absolute path of the saved certificate file cacert.pem
.GuzzleHttp, which this SDK depends on, needs to have the php_curl extension enabled. Check whether the php.ini environment in your environment is enabled. For example, on Linux with PHP 7.1, for services hosted under Apache, you can open /etc/php/7.1/apache2/php.ini
to see whether the extension=php_curl.dll
configuration item has been commented. Please delete the comment before it and restart Apache.
The command is executed normally on the command line, but when it is executed on the web server, the following error is reported:
cURL error 0: The cURL request was retried 3 times and did not succeed. The most likely reason for the failure is that cURL was unable to rewind the body of the request and subsequent retries resulted in the same error. Turn on the debug option to see what went wrong. See https://bugs.php.net/bug.php?id=47204 for more information. (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
This error may occur in different cases. You can run php -r "echo sys_get_temp_dir();"
to print the absolute path of the default system temporary directory and set sys_temp_dir
in php.ini
to this value, and then check whether this error is fixed.
In order to satisfy the need for installation through source code, we previously put the dependent package files in the vendor
directory. However, considering that incompatibility with Composer should not be caused, we had to forbid importing the vendor
directory on GitHub, which resulted in the problem where the git clone
command had to be used to get the vendor
directory. This practice caused confusion for some users not familiar with GitHub. Therefore, starting from v3.0.188, we have temporarily removed the method of installation through source code, and Composer must be used to install the SDK and dependent packages.
Was this page helpful?