Hey there, fellow developer! Ready to supercharge your project management with Jira? Let's dive into building a Jira Software Cloud API integration using PHP. This guide will walk you through the process, assuming you're already familiar with PHP and API basics. We'll keep things concise and focused on the good stuff.
Before we jump in, make sure you've got:
Got all that? Great! Let's get coding.
First things first, let's set up our project:
composer require guzzlehttp/guzzle
Jira uses Basic Auth with an API token. Let's create a reusable function for this:
function getAuthHeader($email, $apiToken) { return base64_encode("$email:$apiToken"); }
Now for the fun part - let's interact with Jira!
$client = new GuzzleHttp\Client(); $response = $client->request('GET', 'https://your-domain.atlassian.net/rest/api/3/issue/ISSUE-KEY', [ 'headers' => [ 'Authorization' => 'Basic ' . getAuthHeader($email, $apiToken), 'Accept' => 'application/json' ] ]);
$response = $client->request('POST', 'https://your-domain.atlassian.net/rest/api/3/issue', [ 'headers' => [ 'Authorization' => 'Basic ' . getAuthHeader($email, $apiToken), 'Accept' => 'application/json', 'Content-Type' => 'application/json' ], 'json' => [ 'fields' => [ 'project' => ['key' => 'PROJECT-KEY'], 'summary' => 'Issue summary', 'issuetype' => ['name' => 'Task'] ] ] ]);
$response = $client->request('PUT', 'https://your-domain.atlassian.net/rest/api/3/issue/ISSUE-KEY', [ 'headers' => [ 'Authorization' => 'Basic ' . getAuthHeader($email, $apiToken), 'Accept' => 'application/json', 'Content-Type' => 'application/json' ], 'json' => [ 'fields' => [ 'summary' => 'Updated summary' ] ] ]);
$response = $client->request('DELETE', 'https://your-domain.atlassian.net/rest/api/3/issue/ISSUE-KEY', [ 'headers' => [ 'Authorization' => 'Basic ' . getAuthHeader($email, $apiToken) ] ]);
Always check the response status and parse the JSON:
$statusCode = $response->getStatusCode(); $body = json_decode($response->getBody(), true); if ($statusCode != 200) { // Handle error }
$jql = urlencode('project = PROJECT-KEY AND status = "In Progress"'); $response = $client->request('GET', "https://your-domain.atlassian.net/rest/api/3/search?jql=$jql", [ 'headers' => [ 'Authorization' => 'Basic ' . getAuthHeader($email, $apiToken), 'Accept' => 'application/json' ] ]);
$response = $client->request('POST', 'https://your-domain.atlassian.net/rest/api/3/issue/ISSUE-KEY/comment', [ 'headers' => [ 'Authorization' => 'Basic ' . getAuthHeader($email, $apiToken), 'Accept' => 'application/json', 'Content-Type' => 'application/json' ], 'json' => [ 'body' => [ 'type' => 'doc', 'version' => 1, 'content' => [ [ 'type' => 'paragraph', 'content' => [ [ 'text' => 'This is a comment', 'type' => 'text' ] ] ] ] ] ] ]);
Don't forget to test! Write unit tests for your functions and integration tests to ensure everything's working smoothly with Jira.
And there you have it! You're now equipped to integrate Jira Software Cloud into your PHP projects. Remember, this is just scratching the surface - Jira's API is powerful and flexible, so don't be afraid to explore further.
For more details, check out the official Jira API documentation. Happy coding, and may your projects be ever organized!