Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 44 additions & 23 deletions Aliyun/Log/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,9 @@
class Aliyun_Log_Client {

/**
* @var string aliyun accessKey
*@var Aliyun_Log_Models_CredentialsProvider credentialsProvider
*/
protected $accessKey;

/**
* @var string aliyun accessKeyId
*/
protected $accessKeyId;

/**
*@var string aliyun sts token
*/
protected $stsToken;
protected $credentialsProvider;

/**
* @var string LOG endpoint
Expand Down Expand Up @@ -64,20 +54,40 @@ class Aliyun_Log_Client {
protected $source;

/**
* Aliyun_Log_Client constructor
* Aliyun_Log_Client constructor.
*
* Either $accessKeyId/$accessKeySecret or $credentialsProvider must be provided.
*
* @param string $endpoint
* LOG host name, for example, http://cn-hangzhou.sls.aliyuncs.com
* @param string $accessKeyId
* aliyun accessKeyId
* @param string $accessKey
* aliyun accessKey
* @param string $token
* aliyun token
* @param Aliyun_Log_Models_CredentialsProvider $credentialsProvider
*/
public function __construct($endpoint, $accessKeyId, $accessKey,$token = "") {
$this->setEndpoint ( $endpoint ); // set $this->logHost
$this->accessKeyId = $accessKeyId;
$this->accessKey = $accessKey;
$this->stsToken = $token;
public function __construct(
string $endpoint,
string $accessKeyId = "",
string $accessKey = "",
string $token = "",
Aliyun_Log_Models_CredentialsProvider $credentialsProvider = null
) {
$this->setEndpoint($endpoint); // set $this->logHost
if (!is_null($credentialsProvider)) {
$this->credentialsProvider = $credentialsProvider;
} else {
if (empty($accessKeyId) || empty($accessKey)) {
throw new Aliyun_Log_Exception("InvalidAccessKey", "accessKeyId or accessKeySecret is empty", "");
}
$this->credentialsProvider = new Aliyun_Log_Models_StaticCredentialsProvider(
$accessKeyId,
$accessKey,
$token
);
}
$this->source = Aliyun_Log_Util::getLocalIp();
}
private function setEndpoint($endpoint) {
Expand Down Expand Up @@ -186,6 +196,17 @@ private function sendRequest($method, $url, $body, $headers) {
* @throws Aliyun_Log_Exception
*/
private function send($method, $project, $body, $resource, $params, $headers) {
$credentials = null;
try {
$credentials = $this->credentialsProvider->getCredentials();
} catch (Exception $ex) {
throw new Aliyun_Log_Exception(
'InvalidCredentials',
'Fail to get credentials:' . $ex->getMessage(),
''
);
}

if ($body) {
$headers ['Content-Length'] = strlen ( $body );
if(isset($headers ["x-log-bodyrawsize"])==false)
Expand All @@ -199,13 +220,13 @@ private function send($method, $project, $body, $resource, $params, $headers) {

$headers ['x-log-apiversion'] = API_VERSION;
$headers ['x-log-signaturemethod'] = 'hmac-sha1';
if(strlen($this->stsToken) >0)
$headers ['x-acs-security-token'] = $this -> stsToken;
if(strlen($credentials->getSecurityToken()) >0)
$headers ['x-acs-security-token'] = $credentials->getSecurityToken();
if(is_null($project))$headers ['Host'] = $this->logHost;
else $headers ['Host'] = "$project.$this->logHost";
$headers ['Date'] = $this->GetGMT ();
$signature = Aliyun_Log_Util::getRequestAuthorization ( $method, $resource, $this->accessKey,$this->stsToken, $params, $headers );
$headers ['Authorization'] = "LOG $this->accessKeyId:$signature";
$signature = Aliyun_Log_Util::getRequestAuthorization ( $method, $resource, $credentials->getAccessKeySecret(), $credentials->getSecurityToken(), $params, $headers );
$headers ['Authorization'] = "LOG ".$credentials->getAccessKeyId().":$signature";

$url = $resource;
if ($params)
Expand Down Expand Up @@ -738,7 +759,7 @@ public function executeProjectSqlJson(Aliyun_Log_Models_ProjectSqlRequest $reque
* Get logs from Log service.
* Unsuccessful opertaion will cause an Aliyun_Log_Exception.
*
* @param Aliyun_Log_Models_GetProjectLogsRequest $request the GetLogs request parameters class.
* @param Aliyun_Log_Models_ProjectSqlRequest $request the GetLogs request parameters class.
* @throws Aliyun_Log_Exception
* @return Aliyun_Log_Models_GetLogsResponse
*/
Expand Down
83 changes: 83 additions & 0 deletions Aliyun/Log/Models/CredentialsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* Copyright (C) Alibaba Cloud Computing
* All rights reserved
*/


class Aliyun_Log_Models_Credentials
{
/**
* @var string
*/
private $accessKeyId;
/**
* @var string
*/
private $accessKeySecret;
/**
* @var string
*/
private $securityToken;

public function __construct(string $accessKeyId, string $accessKeySecret, string $securityToken = '')
{
$this->accessKeyId = $accessKeyId;
$this->accessKeySecret = $accessKeySecret;
$this->securityToken = $securityToken;
}

/**
* @return string accessKeyId
*/
public function getAccessKeyId()
{
return $this->accessKeyId;
}
/**
* @param string $accessKeyId
*/
public function setAccessKeyId(string $accessKeyId)
{
$this->accessKeyId = $accessKeyId;
}
/**
* @return string accessKeySecret
*/
public function getAccessKeySecret()
{
return $this->accessKeySecret;
}
/**
* @param string $accessKeySecret
*/
public function setAccessKeySecret(string $accessKeySecret)
{
$this->accessKeySecret = $accessKeySecret;
}
/**
* @return string securityToken
*/
public function getSecurityToken()
{
return $this->securityToken;
}
/**
* @param string $securityToken
*/
public function setSecurityToken(string $securityToken)
{
$this->securityToken = $securityToken;
}
}

interface Aliyun_Log_Models_CredentialsProvider
{
/**
* @return Aliyun_Log_Models_Credentials
* @throws Exception
*/
public function getCredentials(): Aliyun_Log_Models_Credentials;
}

34 changes: 34 additions & 0 deletions Aliyun/Log/Models/StaticCredentialsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Copyright (C) Alibaba Cloud Computing
* All rights reserved
*/

require_once realpath(dirname(__FILE__) . '/CredentialsProvider.php');


class Aliyun_Log_Models_StaticCredentialsProvider implements Aliyun_Log_Models_CredentialsProvider
{
/**
* @var Aliyun_Log_Models_Credentials
*/
private $credentials;

/**
* @param string $accessKeyId
* @param string $accessKeySecret
* @param string $securityToken
*/
public function __construct(string $accessKeyId, string $accessKeySecret, string $securityToken = '')
{
$this->credentials = new Aliyun_Log_Models_Credentials($accessKeyId, $accessKeySecret, $securityToken);
}
/**
* @return Aliyun_Log_Models_Credentials
*/
public function getCredentials(): Aliyun_Log_Models_Credentials
{
return $this->credentials;
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

## API VERSION

0.6.2
0.6.4

## SDK RELEASE TIME

2018-02-18
2024-05-28

## Introduction

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
]
},
"name": "alibabacloud/aliyun-log-php-sdk",
"version": "0.6.3"
"version": "0.6.4"
}
49 changes: 49 additions & 0 deletions sample/credentialsProviderSample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
echo "Hello, World!";
require_once realpath(dirname(__FILE__) . '/./Log_Autoload.php');

$endpoint = 'cn-hangzhou.log.aliyuncs.com';
$accessKeyId = '';
$accessKey = '';
$token = "";
$project = 'test';
$logstore = 'test';


$credentialsProvider = new Aliyun_Log_Models_StaticCredentialsProvider($accessKeyId, $accessKey, $token);
$client = new Aliyun_Log_Client($endpoint, "", "", "", $credentialsProvider);

$req = new Aliyun_Log_Models_GetLogsRequest($project, $logstore, 1698740109, 1698744321, '', '*', null, null, null, null);

function putLogs(Aliyun_Log_Client $client, $project, $logstore)
{
$topic = 'TestTopic';

$contents = array( // key-value pair
'TestKey' => 'TestContent',
'kv_json' => '{"a": "b", "c": 19021}'
);
$logItem = new Aliyun_Log_Models_LogItem();
$logItem->setTime(time());
$logItem->setContents($contents);
$logitems = array($logItem);
$request = new Aliyun_Log_Models_PutLogsRequest(
$project,
$logstore,
$topic,
null,
$logitems
);

try {
$response = $client->putLogs($request);
} catch (Aliyun_Log_Exception $ex) {
var_dump($ex);
} catch (Exception $ex) {
var_dump($ex);
}
}

putLogs($client, $project, $logstore);
$res = $client->getLogs($req);
var_dump($res->getLogs());