Download address

SDK GitHub:


Follow these steps to start using the Log Service Python SDK quickly.

Step 1. Create an Alibaba Cloud account

For more information, seeSign up with Alibaba Cloud .

Step 2 Obtain an Alibaba Cloud AccessKey

Before using Log Service Python SDK, you must apply for an AccessKey.

 Log on to the Access Key Management page . Select an AccessKey for  for SDK. If you do not have any, create one and make sure the AccessKey is enabled. The AccessKey is used in the following steps and must be kept confidential. For more information about how to use the AccessKey in SDK, seePreparationSDK configuration.

The AccessKey is used in the following steps and must be kept confidential. See Configurations for more information about how to use the AccessKey in SDK.

Step 3. Create a Log Service project and a Logstore

Before using Log Service PHP SDK, you must create a Log Service project and a Logstore in the console.

For how to create a project and a Logstore, see Preparation Create a project in Manage a project and Create a Logstore in Manage a Logstore.

  • •Make sure that you use the same Alibaba Cloud account to obtain the Alibaba Cloud AccessKey and create the Log Service project and Logstore.
  • For more information about the concepts of Log Service such as project and Logstore, see Core concept.
  • A project name must be globally unique in Log Service, and a Logstore name must be unique in the same project.
  • After a project is created, you cannot modify the region  or migrate the project across regions.

Step 4 Install the PHP development environment

The PHP SDK supports PHP 5.2.1 and later versions. You can install any of these versions locally and build the corresponding PHP development environment.

Step 5 Download and install PHP SDK

You must install the PHP SDK after building the PHP development environment.  Follow these steps:
  1. Download the latest PHP SDK package from GitHub.
  2. Decompress the downloaded package to the specified directory. The PHP SDK does not require installation. In addition to the SDK codes, the SDK has a set of third-party dependent packages and an autoloader class for simplified use.  You can follow the steps below to use the SDK directly in your PHP project.

Step 6 Start a new PHP project

You can start using the PHP SDK. To interact with Log Service and  To interact with the Log Service and obtain the relevant output, run the following sample code in a text editor or PHP IDE:

<? php
/* Use the autoloader class to automatically load all required PHP modules. Specify the proper path of the file containing the autoloader class.*/
require_once realpath(dirname(__FILE__) . '/.. /Log_Autoload.php');
$endpoint = ''; // Select the endpoint that matches the region of the project created above
$accessKeyId = 'your_access_key_id';        // Use your Alibaba Cloud access key ID
$accessKey = 'your_access_key';             // Use your Alibaba Cloud access key secret
$project = 'your_project';                  // The name of the project created in the above process
$logstore = 'your_logstore'; // The name of the Logstore created in the preceding step.
$client = new Aliyun_Sls_Client($endpoint, $accessId, $accessKey);
#List the names of all LogStores under the current project
$request = new Aliyun_Sls_Models_ListLogstoresRequest($project);
$response = $client->listLogstores($request);
#Create a LogStore
$req2 = new Aliyun_Log_Models_CreateLogstoreRequest($project,$logstore,3,2);
$res2 = $client -> createLogstore($req2);
#Wait until the LogStore takes effect
#Write logs
$topic = "";
$source = "";
$logitems = array();
for ($i = 0; $i < 5; $i++)
    $contents = array('index1'=> strval($i));
    $logItem = new Aliyun_Log_Models_LogItem();
    Array_push ($ logitems, $ logitem );
$req2 = new Aliyun_Log_Models_PutLogsRequest($project, $logstore, $topic, $source, $logitems);
$res2 = $client->putLogs($req2);
# Drag data immediately
# Traverse shard IDs
$listShardRequest = new Aliyun_Log_Models_ListShardsRequest($project,$logstore);
$listShardResponse = $client -> listShards($listShardRequest);
foreach($listShardResponse-> getShardIds() as $shardId)
    #Obtain the cursor corresponding to each shard ID
    $getCursorRequest = new Aliyun_Log_Models_GetCursorRequest($project,$logstore,$shardId,null, time() - 60);
    $response = $client -> getCursor($getCursorRequest);
    $cursor = $response-> getCursor();
    $count = 100;
        #Read data starting from the cursor.
        $batchGetDataRequest = new Aliyun_Log_Models_BatchGetLogsRequest($project,$logstore,$shardId,$count,$cursor);
        $response = $client -> batchGetLogs($batchGetDataRequest);
        if($cursor == $response -> getNextCursor())
        $logGroupList = $response -> getLogGroupList();
        foreach($logGroupList as $logGroup)
            print ($logGroup->getCategory());
            foreach($logGroup -> getLogsArray() as $log)
                foreach($log -> getContentsArray() as $content)
                    print($content-> getKey().":".$content->getValue()."\t");
        $cursor = $response -> getNextCursor();
#Wait one minute until logs can be queried.
#Query log distribution (NOTE: Ensure that indexes are created before you query logs. The PHP SDK does not provide this interface, so you need to create it on the console.)
$topic = "";
$from = time()-3600;
$to = time();
$res3 = NULL;
while (is_null($res3) || (! $res3->isCompleted()))
    $req3 = new Aliyun_Log_Models_GetHistogramsRequest($project, $logstore, $from, $to, $topic, $query);
    $res3 = $client->getHistograms($req3);
#Query log data.
$res4 = NULL;
while (is_null($res4) || (! $res4->isCompleted()))
    $req4 = new Aliyun_Log_Models_GetLogsRequest($project, $logstore, $from, $to, $topic, $query, 5, 0, False);
    res4 = client.GetLogs(req4);