Skip to content

Instantly share code, notes, and snippets.

@4EverBuilder
Last active August 20, 2020 18:55
Show Gist options
  • Save 4EverBuilder/387466d74573973cf9b9 to your computer and use it in GitHub Desktop.
Save 4EverBuilder/387466d74573973cf9b9 to your computer and use it in GitHub Desktop.
A sample PHP file that can fetch timesheets in Deputy via API
<?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);
}
@p2409
Copy link

p2409 commented Feb 1, 2017

Thanks Ashik - great to see a full, self-contained example of using the Deputy API - which is awesome by the way give it seems to expose everything in the Deputy model.

@dscott0
Copy link

dscott0 commented Jul 23, 2019

Great documentation, very helpful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment