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}publicfunction validate($username,$password){if(!$this->api_token(array('username'=>$username,'password'=>$password,'grant_type'=>'admin'))){returnfalse;}returntrue;}publicfunction 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;returntrue;}}publicfunctionget($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);}}publicfunction 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><formmethod="post">
New Ticket Subject: <inputname="ticketsubject"type="text"placeholder="subject"><br/>
New Ticket Body: <textareaname="ticketbody"></textarea><br/><inputtype="submit"value="Create new ticket"></form>
Question
Seb
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
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.