PHP - Display Categories for a Project
The following code fetches all of the Ticket Categories for a project and populates the result into an array.
define('CBAUTH', 'accountName/userAccount:authToken');
define('APIURL','http://api3.codebasehq.com/projectName');
$catArray = array();
$headers = array('Accept: application/xml', 'Content-type: application/xml');
$apiRequest = APIURL . '/tickets/categories';
/* Fetch the categories from the API */
$c = curl_init();
curl_setopt($c, CURLOPT_URL,$apiRequest);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_USERPWD, CBAUTH);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($c);
curl_close($c);
$myxml = simplexml_load_string($result);
$xmlArray = toArray($myxml);
$catArray = $xmlArray['ticketing-category'];
/* Returns an array from fetched XML */
function toArray($xml) {
$array = json_decode(json_encode($xml), TRUE);
foreach (array_slice($array, 0) as $key => $value) {
if (empty($value)) $array[$key] = NULL;
elseif (is_array($value)) $array[$key] = toArray($value);
}
return $array;
}
Thanks to Luke Davies for this example.