Created
May 22, 2017 03:33
-
-
Save frankreno/c5228e818699fa07951f9104e04eb1ec to your computer and use it in GitHub Desktop.
Example of the Sumo Logic Search Job API using PHP curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$uri = "https://api.us2.sumologic.com/api/v1"; | |
$query = '{"query":"error | count _sourceCategory", "from":"2017-05-18T00:00:00", "to":"2017-05-18T00:15:00", "timeZone": "PST"}'; | |
$http_headers = array( | |
//replace with appropriate access id and access key | |
'Authorization: Basic '. base64_encode("SUMO_ACCESS_ID:SUMO_ACCESS_KEY"), | |
'Accept: application/json', | |
'Content-Type: application/json', | |
); | |
// create the search job | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $uri . "/search/jobs"); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
$result=curl_exec($ch); | |
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close ($ch); | |
if ($status_code != "202") { | |
echo "error creating search job"; | |
} | |
$searchJobID = json_decode($result)->id; | |
// poll search job until complete | |
$state = ""; | |
do { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $uri . "/search/jobs/" . $searchJobID); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
$result=curl_exec($ch); | |
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close ($ch); | |
$state = json_decode($result)->state; | |
} while ($state != "DONE GATHERING RESULTS"); | |
//get the number of messages returned | |
echo "Found ".json_decode($result)->messageCount." messages.\n"; | |
//get the number of records returned, only exists if the query is an aggregate query | |
echo "Found ".json_decode($result)->recordCount." records.\n"; | |
//get the first message | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $uri . "/search/jobs/".$searchJobID."/messages?offset=0&limit=1"); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
$result=curl_exec($ch); | |
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close ($ch); | |
echo "First message:\n"; | |
print_r(json_decode($result)->messages)."\n"; | |
//get the first record | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $uri . "/search/jobs/".$searchJobID."/records?offset=0&limit=1"); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
$result=curl_exec($ch); | |
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close ($ch); | |
echo "First record:\n"; | |
print_r(json_decode($result)->records)."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment