wish-cloud / aliyun-sls
The PHP SDK of Aliyuncs SLS log service
v1.0.0
2025-07-08 08:21 UTC
Requires
- php: >=7.2
Requires (Dev)
README
Introduction
Log Service SDK for Aliyun Log Service(www.aliyun.com/product/sls).
由于官网提供的 PHP SDK 存在一些使用不便,暂时单独发布一份支持 composer 支持命名空间的版本.
安装方法
composer require wish-cloud/aliyun-sls
使用示例
日志查询:
use Wish\AliyunSls\Client; use Wish\AliyunSls\Models\Request\GetLogsRequest; ... $endpoint = 'http://cn-qingdao.log.aliyuncs.com'; $accessKeyId = 'xxxxxx'; $accessKey = 'xxxxxx'; $project = 'project-name'; $logstore = 'logstore-name'; // 日志主题 https://help.aliyun.com/zh/sls/topic $topic = ''; // 时间段 $from = strtotime('2025-06-01 00:00:00'); $to = strtotime('2025-06-01 23:59:59'); //查询语句,可留空。参考 https://help.aliyun.com/zh/sls/query-syntax/ $query = '* | SELECT status, count(*) AS PV GROUP BY status'; $request = new GetLogsRequest($project, $logstore, $from, $to, $topic, $query, 100, 0, False); try { $response = $client->getLogs($request); foreach($response -> getLogs() as $log) { echo 'time:' . $log -> getTime() . ' '; foreach($log -> getContents() as $key => $value){ echo $key.":".$value . ' '; } echo "<br>"; } } catch (\Exception $e) { // }