Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Money Morning Ticket Export</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('.ticket-util').click( function(e) {
e.preventDefault();
var hideLink = $(this);
hideLink.next('.ticket').toggle("slow", function() {
if($(this).is(":visible")) {
hideLink.html('Hide Ticket');
} else {
hideLink.html('Show Ticket');
}
});
});
});
</script>
</head>
<body>
<?php
/**
* A minimal Zendesk API PHP implementation
*
* @package Zendesk
*
* @author Julien Renouard <renouard.julien@gmail.com> (deeply inspired by Darren Scerri <darrenscerri@gmail.com> Mandrill's implemetation)
*
* @version 1.0
*
*/
class Zendesk
{
/**
* API Constructor. If set to test automatically, will return an Exception if the ping API call fails
*
* @param string $apiKey API Key.
* @param string $user Username on Zendesk.
* @param string $subDomain Your subdomain on zendesk, without https:// nor trailling dot.
* @param string $suffix .json by default.
* @param bool $test=true Whether to test API connectivity on creation.
*/
public function __construct($apiKey, $user, $subDomain, $suffix = '.json', $test = false)
{
$this->api_key = $apiKey;
$this->user = $user;
$this->base = 'https://' . $subDomain . '.zendesk.com/api/v2';
$this->suffix = $suffix;
if ($test === true && !$this->test())
{
throw new Exception('Cannot connect or authentice with the Zendesk API');
}
}
/**
* Perform an API call.
*
* @param string $url='/tickets' Endpoint URL. Will automatically add the suffix you set if necessary (both '/tickets.json' and '/tickets' are valid)
* @param array $json=array() An associative array of parameters
* @param string $action Action to perform POST/GET/PUT
*
* @return mixed Automatically decodes JSON responses. If the response is not JSON, the response is returned as is
*/
public function call($url, $json, $action)
{
if (substr_count($url, $this->suffix) == 0)
{
$url .= '.json';
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, $this->base.$url);
curl_setopt($ch, CURLOPT_USERPWD, $this->user."/token:".$this->api_key);
switch($action){
case "POST":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
break;
case "GET":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
break;
case "PUT":
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
default:
break;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
$decoded = json_decode($output);
return is_null($decoded) ? $output : $decoded;
}
/**
* Tests the API using /users/ping
*
* @return bool Whether connection and authentication were successful
*/
public function test()
{
return $this->call('/tickets', '', 'GET');
}
}
$face = new Zendesk( API_KEY, USERNAME, SUBDOMAIN);
// I'm grabbing these from a particular organization
$org_id = ORG_ID;
$test = $face->call('/organizations/'.$org_id.''/tickets', array(), 'GET');
$users = $face->call('/users', array(), 'GET');
$users = $users->users;
$authors = array();
foreach($users as $user) {
$authors[$user->id] = $user->name.' ('.$user->email.')';
}
$tickets = $test->tickets;
print '<h1>Tickets</h1>';
$ids = array();
foreach($tickets as $ticket) {
print '<h2>Subject: '.$ticket->subject.'</h2>';
print '<a href="#" class="ticket-util">Show Ticket</a>';
print '<div class="ticket" style="display: none">';
print '<h3>Created: '.$ticket->created_at.'</h3>';
print '<p><pre>'.htmlentities($ticket->description).'</pre></p>';
print '<h2>Comments</h2>';
print '<div class="comments">';
$comments = $face->call('/tickets/'.$ticket->id.'/comments', array(), 'GET');
$comments = $comments->comments;
foreach($comments as $comment) {
print '<h3>'.$authors[$comment->author_id].'</h3>';
print '<p><pre>'.$comment->html_body.'</pre></p>';
print '<hr>';
}
print '</div>';
print '</div>';
print '<hr>';
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment