Connect Jira Tempo Rest Api with PHP

Back to Blog
Ezeelive Technologies India - Connect Jira Tempo Plugin with PHP

Connect Jira Tempo Rest Api with PHP

Jira is well-known team planning and management application. Thousands of companies using Jira to assign work and team activities. Jira provides facilities to add the plugin as per your requirement. Jira Tempo is one of famous Plugin to get the User, Task, and Timeline Reports. Tempo Restful API builds in Java. So we have built our Jira Class in PHP to use Tempo restful APIs…

Steps for connecting Jira tempo rest API with PHP

1. Make sure CURL is enabled in your server or php.ini file (PHP has phpinfo function to check server setting). Check curl is working with below php code.

function isCurlInstalled() {
	if  (in_array  ('curl', get_loaded_extensions())) {
		return true;
	}
	else {
		return false;
	}
}
if (isCurlInstalled()) {
  echo "installed";
} else {
  echo "NOT installed";
}

2. Create Jira Class (You can create as per your convenient), 3 Private method and CURL functions to connect Jira

class Jira {
    private static $url = "http://192.168.0.59/";	// Tempo URL setup in API TOKEN
    private static $credential = 'ezeelivetechnologies:test123';	// Jira Username and Password
    private static $tempoApiToken = '15ezeelive-7f3f-40f2-a95f-4521918ba183';	// Tempo Token Key

    // Static function to Get Data
    private static function getCURL($rest_url, $https = FALSE, $return_format = FALSE, $full_url = FALSE) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        if (empty($full_url))
            curl_setopt($ch, CURLOPT_URL, Jira::$url . $rest_url);
        else
            curl_setopt($ch, CURLOPT_URL, $rest_url);

        curl_setopt($ch, CURLOPT_USERPWD, Jira::$credential);

        if (!empty($https)) {
            curl_setopt($ch, CURLOPT_SSLVERSION, 3);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        }
        if (!empty($return_format) && $return_format == 'json')
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    // Static function to POST Data	

    private static function postCURL($rest_url, $data = FALSE, $https = FALSE, $return_format = FALSE) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_URL, Jira::$url . $rest_url);
        curl_setopt($ch, CURLOPT_USERPWD, Jira::$credential);

        if (!empty($https)) {
            curl_setopt($ch, CURLOPT_SSLVERSION, 3);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        }
        if (!empty($return_format) && $return_format == 'json')
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

        curl_setopt($ch, CURLOPT_POST, 1);
        if (!empty($data))
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    public static function getAllJiraProjectsKey() {
        $return_data = array();
        $rest_url = "rest/api/2/project";
        $data = Jira::getCURL($rest_url, FALSE, $return_format = 'json');
        if (!empty($data)) {
            $data = json_decode($data, TRUE);
            foreach ($data as $k => $dt) {
                $return_data[] = $dt['key'];
            }
        }
        return $return_data;
    }

    //......	
    //......	
}

3. Now its time to get all the User list associated with Your Jira Project

   public static function getJiraProjectUsers($proj_key) {

        $return_data = array();
        $rest_url = "rest/api/2/project/{$proj_key}/role";
        $data = Jira::getCURL($rest_url, FALSE, $return_format = 'json');

        if (!empty($data)) {
            $data = json_decode($data, TRUE);
            foreach ($data as $k => $dt) {
                if (!is_array($dt)) {
                    $role_data = json_decode(Jira::getCURL($dt, FALSE, $return_format = 'json', TRUE), true);
                    if (!empty($role_data['actors'])) {
                        foreach ($role_data['actors'] as $v) {
                            if(!empty($v['displayName'])){
		                     $return_data[] = array(
		                        'disp_name' => $v['displayName'],
		                        'uid' => !empty($v['id']) ?$v['id']:'',
		                        'jira_auth' => $jauth,
		                        'usr_name' => $v['name'],
		                        'rates' => $rates,
		                        'bill_ty' => $bill_type
		                    );
                            }
                        }
                    }
                }
            }
        }
        return $return_data;
    }

4. Once you get all the User Associated with Your Jira Project. You can get all the Worklog by Jira User or Project Key

  public static function getJiraUserWorklogs($jiraUserName,$fromDate,$toDate) {	// By Jira UserName
        $return_data = array();
        $rest_url = "plugins/servlet/tempo-getWorklog/?addUserDetails=true&dateFrom={$data['sdate']}&dateTo={$data['edate']}&format=xml&diffOnly=false&tempoApiToken=" . Jira::$tempoApiToken . "&addIssueDetails=true&userName={$jiraUserName}";

	$return_data = Jira::getCURL($rest_url);
        return $return_data;
    } 

    public static function getJiraProjectWorklogs($prjkey,$fromDate,$toDate) {		// By Jira Project Key
        $return_data = array();
        $rest_url = "plugins/servlet/tempo-getWorklog/?addUserDetails=true&dateFrom={$data['sdate']}&dateTo={$data['edate']}&format=xml&diffOnly=false&tempoApiToken=" . Jira::$tempoApiToken . "&addIssueDetails=true&projectKey={$prjkey}";

	$return_data = Jira::getCURL($rest_url);
        return $return_data;
    }

5. Final the complete class looks like

class Jira {
    private static $url = "http://192.168.0.59/";	// Tempo URL setup in API TOKEN
    private static $credential = 'ezeelivetechnologies:test123';	// Jira Username and Password
    private static $tempoApiToken = '15ezeelive-7f3f-40f2-a95f-4521918ba183';	// Tempo Token Key

    // Static function to Get Data
    private static function getCURL($rest_url, $https = FALSE, $return_format = FALSE, $full_url = FALSE) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        if (empty($full_url))
            curl_setopt($ch, CURLOPT_URL, Jira::$url . $rest_url);
        else
            curl_setopt($ch, CURLOPT_URL, $rest_url);

        curl_setopt($ch, CURLOPT_USERPWD, Jira::$credential);

        if (!empty($https)) {
            curl_setopt($ch, CURLOPT_SSLVERSION, 3);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        }
        if (!empty($return_format) && $return_format == 'json')
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    // Static function to POST Data	

    private static function postCURL($rest_url, $data = FALSE, $https = FALSE, $return_format = FALSE) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_URL, Jira::$url . $rest_url);
        curl_setopt($ch, CURLOPT_USERPWD, Jira::$credential);

        if (!empty($https)) {
            curl_setopt($ch, CURLOPT_SSLVERSION, 3);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        }
        if (!empty($return_format) && $return_format == 'json')
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

        curl_setopt($ch, CURLOPT_POST, 1);
        if (!empty($data))
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

    public static function getAllJiraProjectsKey() {
        $return_data = array();
        $rest_url = "rest/api/2/project";
        $data = Jira::getCURL($rest_url, FALSE, $return_format = 'json');
        if (!empty($data)) {
            $data = json_decode($data, TRUE);
            foreach ($data as $k => $dt) {
                $return_data[] = $dt['key'];
            }
        }
        return $return_data;
    }

    public static function getJiraProjectUsers($proj_key) {

        $return_data = array();
        $rest_url = "rest/api/2/project/{$proj_key}/role";
        $data = Jira::getCURL($rest_url, FALSE, $return_format = 'json');

        if (!empty($data)) {
            $data = json_decode($data, TRUE);
            foreach ($data as $k => $dt) {
                if (!is_array($dt)) {
                    $role_data = json_decode(Jira::getCURL($dt, FALSE, $return_format = 'json', TRUE), true);
                    if (!empty($role_data['actors'])) {
                        foreach ($role_data['actors'] as $v) {
                            if(!empty($v['displayName'])){
		                     $return_data[] = array(
		                        'disp_name' => $v['displayName'],
		                        'uid' => !empty($v['id']) ?$v['id']:'',
		                        'jira_auth' => $jauth,
		                        'usr_name' => $v['name'],
		                        'rates' => $rates,
		                        'bill_ty' => $bill_type
		                    );
                            }
                        }
                    }
                }
            }
        }
        return $return_data;
    }

    public static function getJiraUserWorklogs($jiraUserName,$fromDate,$toDate) {	// By Jira UserName
        $return_data = array();
        $rest_url = "plugins/servlet/tempo-getWorklog/?addUserDetails=true&dateFrom={$data['sdate']}&dateTo={$data['edate']}&format=xml&diffOnly=false&tempoApiToken=" . Jira::$tempoApiToken . "&addIssueDetails=true&userName={$jiraUserName}";

	$return_data = Jira::getCURL($rest_url);
        return $return_data;
    } 

    public static function getJiraProjectWorklogs($prjkey,$fromDate,$toDate) {		// By Jira Project Key
        $return_data = array();
        $rest_url = "plugins/servlet/tempo-getWorklog/?addUserDetails=true&dateFrom={$data['sdate']}&dateTo={$data['edate']}&format=xml&diffOnly=false&tempoApiToken=" . Jira::$tempoApiToken . "&addIssueDetails=true&projectKey={$prjkey}";

	$return_data = Jira::getCURL($rest_url);
        return $return_data;
    }	 
}

If you have any query, suggestion or help on this jira tempo rest API, please comment or mail us on info[at]ezeelive[dot]com.

Summary
Connect Jira Tempo Rest Api with PHP
Article Name
Connect Jira Tempo Rest Api with PHP
Description
Jira tempo rest api provides worklog for period, user and projectwise, Ezeelive Technologies has build class in PHP to connect and use Jira Tempo apis.
Author
Publisher Name
Ezeelive Technologies
Publisher Logo

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back to Blog