Last active
August 20, 2020 18:55
-
-
Save 4EverBuilder/387466d74573973cf9b9 to your computer and use it in GitHub Desktop.
A sample PHP file that can fetch timesheets in Deputy via API
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 | |
// Just run via command line php -q sample_timesheet_fetch.php | |
date_default_timezone_set("Australia/Sydney"); | |
$actual_db = array(); | |
$total_count = 0; | |
// get all timesheets | |
//$search = array('emp_search' => array('field' => 'Employee' , 'data'=> "1" , 'type' => 'ge' ) ); | |
// get just everything since last 1 week | |
$search = array('created_search' => array('field' => 'Created' , 'data'=> date("Y-m-d 00:00:00" , strtotime("-1 week")) , 'type' => 'ge' ) ); | |
// Example below searches all timesheets in a month for employee called Bradley | |
//$search = array( | |
// 'from_date_search' => array('field' => 'Date' , 'data'=> "2005-08-01" , 'type' => 'ge' ) | |
//, 'to_date_search' => array('field' => 'Date' , 'data'=> "2015-08-31" , 'type' => 'le' ) | |
//, 'employee_search' => array('field' => 'DisplayName' , 'data'=> "%Bradley%" , 'type' => 'lk' , 'join' => 'EmployeeObject') | |
//); | |
while(true){ | |
$fetched = dp_api("YOUR DOMAIN HERE", "resource/Timesheet/QUERY" , "YOUR TOKEN HERE", array('join'=> array('EmployeeObject' , 'OperationalUnitObject') , 'search' => $search , 'max' => 500 , 'start' => $total_count )); | |
$last_count = count($fetched); | |
$total_count += count($fetched); | |
$actual_db = array_merge($actual_db , $fetched); | |
if($last_count < 500) | |
break; | |
} | |
// timesheets here | |
print_r($actual_db); | |
/** | |
* Get Resource from an url | |
* | |
* @return string | |
*/ | |
function dp_wget($url , $postvars = null , $curlOpts = array()){ | |
// Purpose : refresh session token every hour | |
$piTrCurlHandle = curl_init(); | |
curl_setopt($piTrCurlHandle, CURLOPT_HTTPGET, 1); | |
curl_setopt($piTrCurlHandle, CURLOPT_RESUME_FROM, 0); | |
curl_setopt($piTrCurlHandle, CURLOPT_URL, $url); | |
curl_setopt($piTrCurlHandle, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($piTrCurlHandle, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt($piTrCurlHandle, CURLOPT_SSL_VERIFYHOST, 0); | |
curl_setopt($piTrCurlHandle, CURLOPT_SSL_VERIFYPEER, 0); | |
curl_setopt($piTrCurlHandle, CURLOPT_TIMEOUT, 500); // 500 secs | |
if($postvars){ | |
curl_setopt($piTrCurlHandle, CURLOPT_POST, 1); | |
curl_setopt($piTrCurlHandle, CURLOPT_POSTFIELDS, $postvars); | |
} | |
if($curlOpts) | |
foreach($curlOpts as $opt=>$value) | |
curl_setopt($piTrCurlHandle, $opt, $value); | |
$data = curl_exec($piTrCurlHandle); | |
return $data; | |
} | |
/** | |
* Call Deputy API | |
*/ | |
function dp_api($endpoint , $url , $token , $postvars = null){ | |
$get = dp_wget("https://" . $endpoint . "/api/v1/" . $url | |
, $postvars?json_encode($postvars):null | |
, array( | |
CURLOPT_HTTPHEADER => array( | |
'Content-type: application/json' | |
, 'Accept: application/json' | |
, 'Authorization: OAuth ' . $token | |
, 'dp-meta-option: none' | |
) | |
) | |
); | |
return json_decode($get , true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great documentation, very helpful, thanks!