edit-icon download-icon

PHP SDK

Last Updated: Apr 09, 2018

Log Service PHP SDK allows PHP developers to use Alibaba Cloud Log Service conveniently.

Download address

SDK GitHub address: https://github.com/aliyun/aliyun-log-php-sdk

Procedure

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

Step 1 Create an Alibaba Cloud account

For more information, see Sign up with Alibaba Cloud.

Step 2 Obtain an Alibaba Cloud AccessKey

Before using Log Service PHP SDK, you must apply for an Alibaba Cloud AccessKey.

Log on to the Alibaba Cloud Access Key Management page. Select an AccessKey for SDK. If you do not have any, create one and make sure the AccessKey is enabled. For how to create an AccessKey, see Create and enable AccessKey in Preparation.

The AccessKey is used in the following steps and must be kept confidential. For more information about how to use the AccessKey in SDK, see SDK configuration.

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 Create a project in Manage a project and Create a Logstore in Manage a Logstore.

Note:

  • 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 is a software development kit and does not require additional installation. In addition to the SDK codes, the SDK has a set of third-party dependent packages and an autoloader class for simplified use. Follow these steps 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 obtain the relevant output, run the following sample code in a text editor or PHP IDE.

  1. <?php
  2. /* Use the autoloader class to automatically load all required PHP modules. Specify the proper path of the file containing the autoloader class.*/
  3. require_once realpath(dirname(__FILE__) . '/../Log_Autoload.php');
  4. $endpoint = 'cn-hangzhou.sls.aliyuncs.com'; // Select the endpoint that matches the region of the project created in the preceding step.
  5. $accessKeyId = 'your_access_key_id'; // Use your Alibaba Cloud AccessKey ID.
  6. $accessKey = 'your_access_key'; // Use your Alibaba Cloud AccessKey Secret.
  7. $project = 'your_project'; // The name of the project created in the preceding step.
  8. $logstore = 'your_logstore'; // The name of the Logstore created in the preceding step.
  9. $client = new Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey);
  10. #List the names of all the Logstores in the current project.
  11. $req1 = new Aliyun_Log_Models_ListLogstoresRequest($project);
  12. $res1 = $client->listLogstores($req1);
  13. var_dump($res1);
  14. #Create a Logstore.
  15. $req2 = new Aliyun_Log_Models_CreateLogstoreRequest($project,$logstore,3,2);
  16. $res2 = $client -> createLogstore($req2);
  17. #Wait until the Logstore takes effect.
  18. sleep(60);
  19. #Write logs.
  20. $topic = "";
  21. $source = "";
  22. $logitems = array();
  23. for ($i = 0; $i < 5; $i++)
  24. {
  25. $contents = array('index1'=> strval($i));
  26. $logItem = new Aliyun_Log_Models_LogItem();
  27. $logItem->setTime(time());
  28. $logItem->setContents($contents);
  29. array_push($logitems, $logItem);
  30. }
  31. $req2 = new Aliyun_Log_Models_PutLogsRequest($project, $logstore, $topic, $source, $logitems);
  32. $res2 = $client->putLogs($req2);
  33. var_dump($res2);
  34. #Drag data immediately.
  35. #Traverse shard IDs.
  36. $listShardRequest = new Aliyun_Log_Models_ListShardsRequest($project,$logstore);
  37. $listShardResponse = $client -> listShards($listShardRequest);
  38. foreach($listShardResponse-> getShardIds() as $shardId)
  39. {
  40. #Obtain the cursor corresponding to each shard ID.
  41. $getCursorRequest = new Aliyun_Log_Models_GetCursorRequest($project,$logstore,$shardId,null, time() - 60);
  42. $response = $client -> getCursor($getCursorRequest);
  43. $cursor = $response-> getCursor();
  44. $count = 100;
  45. while(true)
  46. {
  47. #Read data starting from the cursor.
  48. $batchGetDataRequest = new Aliyun_Log_Models_BatchGetLogsRequest($project,$logstore,$shardId,$count,$cursor);
  49. var_dump($batchGetDataRequest);
  50. $response = $client -> batchGetLogs($batchGetDataRequest);
  51. if($cursor == $response -> getNextCursor())
  52. {
  53. break;
  54. }
  55. $logGroupList = $response -> getLogGroupList();
  56. foreach($logGroupList as $logGroup)
  57. {
  58. print ($logGroup->getCategory());
  59. foreach($logGroup -> getLogsArray() as $log)
  60. {
  61. foreach($log -> getContentsArray() as $content)
  62. {
  63. print($content-> getKey().":".$content->getValue()."\t");
  64. }
  65. print("\n");
  66. }
  67. }
  68. $cursor = $response -> getNextCursor();
  69. }
  70. }
  71. #Wait one minute until logs can be queried.
  72. sleep(60);
  73. #Query log distribution (Note: Make sure that indexes are created before you query logs. The PHP SDK does not provide this interface, so you must create them in the console.)
  74. $topic = "";
  75. $query='';
  76. $from = time()-3600;
  77. $to = time();
  78. $res3 = NULL;
  79. while (is_null($res3) || (! $res3->isCompleted()))
  80. {
  81. $req3 = new Aliyun_Log_Models_GetHistogramsRequest($project, $logstore, $from, $to, $topic, $query);
  82. $res3 = $client->getHistograms($req3);
  83. }
  84. var_dump($res3);
  85. #Query log data.
  86. $res4 = NULL;
  87. while (is_null($res4) || (! $res4->isCompleted()))
  88. {
  89. $req4 = new Aliyun_Log_Models_GetLogsRequest($project, $logstore, $from, $to, $topic, $query, 5, 0, False);
  90. $res4 = $client->getLogs($req4);
  91. }
  92. var_dump($res4);
Thank you! We've received your feedback.