All Products
Search
Document Center

Simple Log Service:Get started with Simple Log Service SDK for PHP

Last Updated:Oct 26, 2023

This topic describes how to get started with Simple Log Service SDK for PHP and perform common operations.

Prerequisites

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

  • Simple Log Service SDK for PHP is installed. For more information, see Install Simple Log Service SDK for PHP.

Sample code

  • Create a client for Simple Log Service

    Aliyun_Log_Client is a PHP client that you can use to manage Simple Log Service resources, such as projects and Logstores. Before you can use Simple Log Service SDK for PHP to initiate a request, you must initialize a client instance.

    // The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
    $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); 
  • Create a Logstore

    Simple Log Service SDK for PHP provides a variety of sample code for your reference and use. The following sample code provides an example on how to create a Logstore:

    // Specify the project name, Logstore name, retention period of data, and number of shards. If you set the retention period of data to 3650, data is permanently stored. 
    $req2 = new Aliyun_Log_Models_CreateLogstoreRequest('test-project','test-logstore',3,2);  
    $res2 = $client -> createLogstore($req2);

    For more information, see Alibaba Cloud Simple Log Service SDK for PHP.