Categories

Forum Search

Search terms:

TopicsCommentsUsers

The Intervals Forum is Closed.This forum has been replaced with a help section located at help.myintervals.com.
We recommend checking the help section, as the forums are no longer being updated. If you have any questions, please don't hesitate to contact our support team.

API PHP Code Sample: Timer Manipulation

The following PHP class shows how to create, pause, restart, and delete a timer. It requires PHP5 and the cURL and SimpleXMLElement libraries. It is a bare bones example that you are free to use in your application, modify to your programming language, extend, or distribute as you wish. It contains no error handling, which you are advised to put in your application when making API requests, so you will have to write your own.

Documentation for the timer resource can be found here:https://www.myintervals.com/api/resource.php?r=timer

// 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);