<?php
define('TOKEN',''); // put your token here
define('BASEURL','https://api.myintervals.com/');
class Timer {
private $timerid;
private $ch;
function __construct() {
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_HEADER, 0);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($this->ch, CURLOPT_USERPWD, TOKEN.':x');
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Accept: application/xml", "Content-type: application/xml"));
}
function __descruct() {
curl_close($this->ch);
}
public function setTimerId($id) {
$this->timerid = $id;
}
public function getTimerId() {
return $this->timerid;
}
// Creates a timer.
public function createTimer() {
// Get information about me so that we can start my timer. If you know your personid, you can skip this step
curl_setopt($this->ch, CURLOPT_URL, BASEURL.'me/');
// load the returned value into a SimpleXML parser.
// you'll probably want to space this out and add your own error handling here
$me = new SimpleXMLElement(curl_exec($this->ch), LIBXML_NOCDATA);
$personid = (int)$me->me->item->id;
$timenow = gmdate('Y-m-d H:i:s');
curl_setopt($this->ch, CURLOPT_URL, BASEURL.'timer/');
// we should use DOMDocument or SimpleXMLElement to create well-formatted XML, but for simplicity and so you can
// see the output being sent to the server, I'm going to send it as a string.
$newTimer =<<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<timer>
<personid>$personid</personid>
<starttime>$timenow</starttime>
</timer>
EOF;
curl_setopt($this->ch, CURLOPT_HTTPGET, 0);
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $newTimer);
$timer = new SimpleXMLElement(curl_exec($this->ch), LIBXML_NOCDATA);
$this->setTimerId((int)$timer->timer->id);
return true;
}
public function pauseTimer() {
// Now let's pause the timer. To do this, we'll get the most recent information about how long the timer's been running.
// If you want to set the time manually, you can set the `time` field to whatever you want and skip this step.
curl_setopt($this->ch, CURLOPT_POST, 0);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, '');
curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
curl_setopt($this->ch, CURLOPT_URL, BASEURL.'timer/'.$this->getTimerId());
$timer = new SimpleXMLElement(curl_exec($this->ch), LIBXML_NOCDATA);
$timelapsed = (float)$timer->timer->timelapsed;
// We'll set the `starttime` field to null to indicate a paused timer.
$pauseTimer =<<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<timer>
<time>$timelapsed</time>
<starttime />
</timer>
EOF;
// Now reset the cURL options to POST to pause the timer.
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_HTTPGET, 0);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $pauseTimer);
curl_exec($this->ch);
return true;
}
public function restartTimer() {
// Now, let's restart the timer the same way we started it originally (only we don't need personid, as it's already set)
curl_setopt($this->ch, CURLOPT_URL, BASEURL.'timer/'.$this->getTimerId());
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_HTTPGET, 0);
$timenow = gmdate('Y-m-d H:i:s');
$restartTimer =<<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<timer>
<starttime>$timenow</starttime>
</timer>
EOF;
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $restartTimer);
curl_exec($this->ch);
return true;
}
public function deleteTimer() {
// Now, let's get rid of the timer we created. This should be super simple.
curl_setopt($this->ch, CURLOPT_POST, 0);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, '');
curl_setopt($this->ch, CURLOPT_HTTPGET, 0);
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_exec($this->ch);
return true;
}
}
?>
$timer = new Timer;
$timer->createTimer();
$timer->pauseTimer();
$timer->restartTimer();
$timer->deleteTimer();
$timer = new Timer;
$timer->setTimerId(1463993);
$timer->pauseTimer();
1 to 2 of 2