Perform common Simple Log Service operations with the SDK for PHP, such as managing projects and Logstores.
Prerequisites
-
Simple Log Service is activated. Activate Simple Log Service.
-
An AccessKey pair has been created. For more information, see AccessKey pair.
The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. This poses a high security threat. We strongly recommend that you create and use a Resource Access Management (RAM) user to make API calls or perform routine O&M. The RAM user must have the required permissions to manage Simple Log Service resources. For more information, see Grant permissions to a RAM user.
-
The Simple Log Service SDK for PHP is installed. For more information, see Install the PHP SDK.
Sample code
The following example creates a test.php file in the same directory as aliyun-log-php-sdk-master and calls an API operation to create a Logstore. Aliyun_Log_Client is the PHP client for Simple Log Service that manages resources such as projects and Logstores. Initialize a client instance before sending requests. For more sample code, see Aliyun Log PHP SDK.
<?PHP
require_once realpath(dirname(__FILE__).'/aliyun-log-php-sdk-master/Log_Autoload.php');
class test
{
public static function main()
{
// The endpoint of Simple Log Service. This example uses the endpoint for the China (Hangzhou) region. Replace the value with the endpoint for your region.
$endpoint = 'cn-hangzhou.log.aliyuncs.com';
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
$accessKeyId = getenv('ALIBABA_CLOUD_ACCESS_KEY_ID');
$accessKey = getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET');
// Create a Simple Log Service client.
$client = new Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey);
// The project name.
$project = 'aliyun-test-project';
// The Logstore name.
$logstore = 'aliyun-test-logstore';
// The data retention period in days. If you set this parameter to 3650, the data is permanently stored.
$infrequentAccessTTL = 30;
// The number of shards.
$shardCount = 2;
// Create a Logstore.
$req2 = new Aliyun_Log_Models_CreateLogstoreRequest($project, $logstore, $infrequentAccessTTL, $shardCount);
$res2 = $client->createLogstore($req2);
}
}
test::main();