Jump to content
  • 0

Get tickets via API


Seb

Question

A client needed some guidance putting a quick API call together and our API docs aren't yet particularly user-friendly, so for anyone else's benefit here is a very quick and dirty proof of concept of how to get tickets and create a ticket via the API

<?php

class upmindAuth {

    public $api_url;
    public $origin;
    public $token;
    
    function __construct($origin){
        $this->api_url = 'api.upmind.io'; //live api url
        $this->origin = $origin; // Brand Origin
    }
    
    public function validate($username,$password){
        if (!$this->api_token(array('username'=>$username,'password'=>$password,'grant_type'=>'admin'))){
            return false;
        }
        return true;
    }
    
    public function api_token($params){
    
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => "https://".$this->api_url."/oauth/access_token",
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => "",
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 30,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => "POST",
          CURLOPT_POSTFIELDS => json_encode($params),
          CURLOPT_HTTPHEADER => array(
            "accept: application/vnd.pub.v1+json",
            "cache-control: no-cache",
            "content-type: application/json",
            "origin: $this->origin",
          ),
        ));
    
        $response = curl_exec($curl);
        $err = curl_error($curl);
    
        if ($err) {
            return array('success'=>false,'error'=>"cURL Error #:" . $err);
        } else {
            $actor = json_decode($response);           
            $this->token = $actor->access_token;
            return true;
        }
    }
    
    public function get($endpoint){
      
        $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => "https://".$this->api_url.$endpoint,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
          "authorization: Bearer ".$this->token,
          "cache-control: no-cache",
          "origin: ".$this->origin,
        ),
      ));
    
      $response = curl_exec($curl);
    
      $err = curl_error($curl);
      
      if ($err) {
        echo "cURL Error #:" . $err;
      } else {
          return json_decode($response);
      }
    }
    
    public function post($endpoint,$params){
    
      $curl = curl_init();
      curl_setopt_array($curl, array(
      CURLOPT_URL => "https://".$this->api_url.$endpoint,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => $params,
      CURLOPT_HTTPHEADER => array(
        "authorization: Bearer ".$this->token,
        "cache-control: no-cache",
        "origin: ".$this->origin,
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
        return json_decode($response);
    }
    }
    
}

// Upmind API Creds
$instance = "https://xxxxxxxxx.upmind.app"; // Your Upmind Brand URL (E.g. https://my.domain.com)
$user = "xxxx"; // username
$pass = "xxxx"; // password

// Now we get tickets from Upmind
$upmind = new upmindAuth($instance);
if (!$upmind->validate($user,$pass)){
    echo "Username and password incorrect"; die;
}

if (isset($_POST['ticketsubject'])){
    $clientid = "xxx-xxx-xxx"; // ID of your client 
    $endpoint  = "/api/admin/tickets";
    $ticket = $upmind->post($endpoint,array('client_id'=>$clientid,'subject'=>$_POST['ticketsubject'],'body'=>$_POST['ticketbody']));
    if ($ticket->status == 'ok') { 
        echo "<p>Ticket with reference " . $ticket->data->reference . " created</p>"; 
    } else {
        echo "<p>Error</p>";
    }
}

// Single API call to get the data we need
$endpoint = "/api/admin/tickets?with=messages";
$output = $upmind->get($endpoint);

foreach ($output->data as $o){
    ?>Ticket Ref: <?=$o->reference?>: <?=$o->subject?> (<?php echo sizeof($o->messages);?>)<br /><?php
}

?>
<h2>Create new ticket</h2>
<form method="post">
    New Ticket Subject: <input name="ticketsubject" type="text" placeholder="subject"><br />
    New Ticket Body: <textarea name="ticketbody"></textarea><br />
    <input type="submit" value="Create new ticket">
</form>

 

  • Like 2
Link to comment
Share on other sites

2 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...