Skip to content

Instantly share code, notes, and snippets.

@mpstenson
Created August 13, 2015 13:58
Show Gist options
  • Save mpstenson/66668cb60114002554ae to your computer and use it in GitHub Desktop.
Save mpstenson/66668cb60114002554ae to your computer and use it in GitHub Desktop.
<?php
// Be sure to send milliseconds
$now = time() * 1000; //gives milliseconds
$starttime = $now - 604800000; //7 days ago.
// endpoint for aviso orientation complete data. Requires a startdate in milliseconds and an end date in milliseconds
$aviso_url = 'https://youravisoinstall/aviso/api/2/person/orientationCompleteDate?startDate='.$starttime.'&endDate='.$now;//***Edit This Line
//Aviso API key generated in admin
$aviso_password = ''; //***Edit This Line
// The salt is for one time use, it must be generated for every request
$salt = base64_encode(openssl_random_pseudo_bytes(30));
// The password is never sent to aviso, we simply check that the signature generated is the same
$signature = hmac_sha256($aviso_password, ($now . $salt));
// Bundle the salt, timestamp and signature into a json array
$json_args = json_encode(array('salt' => $salt, 'timestamp' => $now, 'signature' => $signature));
// Connect to Aviso with Curl using a post request, and json content type
$ch = curl_init($aviso_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type: application/json', 'Content-Length: ' . strlen($json_args)));
$aviso_result = curl_exec($ch);
// Grab the http status code to verify success of the call
$aviso_result_status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$aviso_result_err = curl_error($ch);
curl_close($ch);
//function creates a hash of the timestamp and salt using the password as the key.
function hmac_sha256($password, $timestamp_plus_salt){
return hash_hmac('sha256', ($timestamp_plus_salt), $password);
}
//open up a file stream to write results to this will get picked up by informatica cloud and piped info sf
$fp = fopen("c:/orientationcompleteresults/results.csv","w");
//check to make sure we had a good result
if($aviso_result_status_code == 200){
//Take the string returned json decode it to get an object that we can recurse over
$json_result=json_decode($aviso_result);
//Write the column headers to the file.
$headerline ="StudentID, CompletedDate\r\n";
fputs($fp, $headerline);
//For each item in the data object let's loop
foreach($json_result->data as $item) {
//Get completion date in senconds instead of milliseconds
$completionseconds = $item->orientationCompleteDate / 1000;
//Prep the line to be written to csv
$line = $item->institutionId.','.date("m-d-Y", $completionseconds)."\r\n";
//Write the line.
fputs($fp, $line);
}
}else{
//If we get an error let's echo it out.
echo $aviso_result_err;
}
//Clean up our file stream
fclose($fp);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment