tencent cloud

PHP SDK Log Upload
Last updated: 2025-11-07 17:40:37
PHP SDK Log Upload
Last updated: 2025-11-07 17:40:37
This article introduces how to quickly get started with the Cloud Log Service (CLS) PHP SDK to implement log upload. For more detailed information on SDK usage, see the code repository tencentcloud-cls-sdk-php.

Prerequisites

Create and obtain TencentCloud API key information, including accessKeyId and accessKey. For key information acquisition, please visit API Key Management.
Please ensure the associated account has appropriate SDK log upload permission.

Preparing the Development Environment

Install PHP. For details, see PHP Official Website.
CLS PHP SDK supports PHP 5.6.0 and above versions. You can run the following command to check your installed PHP Version.
php -v
Execute the following command to install the environment dependency:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Installing the PHP SDK

Install the Tencent Cloud CLS PHP SDK using Composer.
composer require tencentcloud/tencentcloud-cls-sdk-php

Request Parameters

Variable
Type
Required
Description
endpoint
String
Yes
Domain information, fill in the domain name in the API Log Upload Tab of available region.
accessKeyId
String
Yes
TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has appropriate SDK log upload permission.
accessKey
String
Yes
TencentCloud API key information. For key information acquisition, please visit API Key Management. Please ensure the associated account has appropriate SDK log upload permission.
topicId
String
Yes
Log topic ID info.
token
String
No
Temporary key token. If needed, fill in this parameter to use a temporary key. For temporary key token retrieval, see Temporary Key.

Upload Example Code for Logs

Take the PHP SDK as an example to show the log upload operation by calling the SDK. The sample code is as follows.
It is not recommended to store TencentCloud API key information in plaintext in project code. You can dynamically obtain TencentCloud API key information through environment variables. For details, see configure environment variables.
<?php

require_once __DIR__ . '/vendor/autoload.php'; // Import Composer autoloader

use TencentCloud\\Cls\\Models\\Request\\PutLogsRequest;
use TencentCloud\\Cls\\Models\\LogItem;
use TencentCloud\\Cls\\Client;
use TencentCloud\\Cls\\TencentCloudLogException;

function putLogs($client, $topicId) {
$contents = array(
//Set the log key and value content, the previous is the key, the following is the value
'TestKey' => 'TestContent',
'test2' => 'helloworld'
);

$logItem = new LogItem();
$logItem->setTime(time()); // Set log time
$logItem->setContents($contents); // Set log content
$logItems = array($logItem); // Create log item array

$request = new PutLogsRequest($topicId, null, $logItems); // Create request

try {
$response = $client->putLogs($request); // Send request
var_dump($response->getRequestId()); // Output request ID
} catch (TencentCloudLogException $ex) {
var_dump($ex); // Capture and output Tencent Cloud log error
} catch (Exception $ex) {
var_dump($ex); // Capture and output other exceptions
}
}

// Fill in the domain name, see the domain name in the API Log Upload Tab at the link: https://www.tencentcloud.com/document/product/614/18940?from_cn_redirect=1#.E5.9F.9F.E5.90.8D
$endpoint = 'ap-xxxxxxxx.cls.tencentcs.com';

// Fill in TencentCloud API key information. For key information acquisition, please visit: https://console.tencentcloud.com/cam/capi
// Please ensure the associated account has log upload permissions. For the permission configuration guide, see: https://www.tencentcloud.com/document/product/614/68374?from_cn_redirect=1#.E4.BD.BF.E7.94.A8-api-.E4.B8.8A.E4.BC.A0.E6.95.B0.E6.8D.AE
// This example retrieves from environmental variables. For the environment variable configuration guide, see: https://www.tencentcloud.com/document/product/614/113851?from_cn_redirect=1
$accessKeyId = getenv('TENCENTCLOUD_SECRET_ID');
$accessKey = getenv('TENCENTCLOUD_SECRET_KEY');

// Set the topic ID for log upload, replace with your Topic ID
$topicId = 'YOUR_TOPIC_ID';

// Temporary key token, fill in if needed. Temporary key configuration guide: https://www.tencentcloud.com/document/product/614/87777?from_cn_redirect=1
$token = "";

// Create a client.
$client = new Client($endpoint, $accessKeyId, $accessKey, $token);

// Invoke function to send logs
putLogs($client, $topicId);

Conclusions

Through these steps, you can quickly use the Tencent Cloud CLS PHP SDK to complete log upload. If you encounter any issues, contact us to get help.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback