Not signed in (Sign In)
The Intervals Forum is read-only
Please head to help.myintervals.com for help articles and guides. If you have any questions, please contact our support team.

API

API PHP Code Sample: Timer Manipulation

Bottom of Page

1 to 2 of 2

  1.  
  1.  

    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



    <?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;
    }
    }

    ?>

  2.  

    Forgot to mention that this class can be used as follows:


    $timer = new Timer;
    $timer->createTimer();
    $timer->pauseTimer();
    $timer->restartTimer();
    $timer->deleteTimer();


    OR (for an existing timer)


    $timer = new Timer;
    $timer->setTimerId(1463993);
    $timer->pauseTimer();

Comments are closed.
For more Intervals help documentation, please visit help.myintervals.com