From d93b6f74382e7bf41017fa64387cd356c5127781 Mon Sep 17 00:00:00 2001 From: Rineez Ahmed Date: Tue, 21 Nov 2017 22:04:28 +0530 Subject: [PATCH 1/3] Add support for token based api authentication https://www.loggly.com/docs/token-based-api-authentication/ --- Loggly.php | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/Loggly.php b/Loggly.php index e1d059d..0bae03b 100644 --- a/Loggly.php +++ b/Loggly.php @@ -24,6 +24,7 @@ class Loggly { public $subdomain = ''; public $username = ''; public $password = ''; + public $apiToken = ''; public $inputs = array(); public function __construct() {} @@ -47,7 +48,13 @@ public function makeRequest($path, $params = null, $method = 'GET') { } # set HTTP headers - curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password); + $headers = []; + if($this->username || $this->password){ + curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password); + } + if($this->apiToken){ + $headers[] = 'Authorization: bearer ' . $this->apiToken; + } curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); @@ -72,7 +79,11 @@ public function makeRequest($path, $params = null, $method = 'GET') { # satisfy content length header requirement for PUT if ($method === 'PUT') { - curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($qs))); + $headers[] = 'Content-Length: ' . strlen($qs); + } + + if(!empty($headers)){ + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } $result = curl_exec($curl); From ce713dbd0e81f4689c7d37eaa3ee097e946b7ec6 Mon Sep 17 00:00:00 2001 From: Rineez Date: Tue, 21 Nov 2017 22:53:06 +0530 Subject: [PATCH 2/3] Update README.md --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9cda4cc..3f0d4c6 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,20 @@ The first step is to instantiate a new instance of the Loggly class: $loggly->subdomain = ''; +Authenticate using username & password +======================================== + + $loggly->username = 'demo'; $loggly->password = '42ftw'; +Authenticate using [API Token](https://www.loggly.com/docs/token-based-api-authentication/) +============================== + + + $loggly->apiToken = 'c1f1e7e4-61e7-4b19-83f4-a26dd61dca3c'; + Input and Device Methods ======================== @@ -157,4 +167,4 @@ Support ======= Have questions? -Contact support@loggly.com (please include your subdomain) \ No newline at end of file +Contact support@loggly.com (please include your subdomain) From cda28149176941e6206ec94e85807bf0f67657ab Mon Sep 17 00:00:00 2001 From: Rineez Ahmed Date: Wed, 22 Nov 2017 00:08:13 +0530 Subject: [PATCH 3/3] Upgrade search method to use version 2 of api(apiv2). None of the other old features seems to have an equivalent endpoint anymore. --- Loggly.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Loggly.php b/Loggly.php index 0bae03b..c11f42e 100644 --- a/Loggly.php +++ b/Loggly.php @@ -137,7 +137,7 @@ public function disableDiscovery($inputId) { public function search($q, $params = null) { $params['q'] = $q; - return $this->makeRequest('/api/search', $params); + return $this->makeRequest('/apiv2/events/iterate', $params); } public function facet($q, $facet = 'date', $params = null) {