diff --git a/Loggly.php b/Loggly.php index e1d059d..c11f42e 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); @@ -126,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) { 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)