Integrating with Jira's REST API using PHP

I’m attempting to interface with Jira’s REST API through PHP but I’m encountering some difficulties. My code runs without providing any error or success response.

Here’s the code I’m using:

<?php
include_once "data_loader.php";

$dataForIssue = loadJsonData("collector.json");
$jiraApiUrl = "http://jira.mycompany.com:8091/rest/api/2/issue/";
$jsonData = json_encode($dataForIssue);

$curlSession = curl_init($jiraApiUrl);
curl_setopt($curlSession, CURLOPT_HEADER, 0);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlSession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curlSession, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curlSession, CURLOPT_ENCODING, "");
curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curlSession, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curlSession, CURLOPT_URL, $jiraApiUrl);

$apiResponse = curl_exec($curlSession);
curl_close($curlSession);

$responseArray = json_decode($apiResponse, TRUE);
print_r($responseArray);

My JSON data is structured as follows:

{
  "fields": {
    "project": {
      "key": "PROJ"
    },
    "summary": "Issue creation for testing API",
    "description": "This is a sample issue made through REST API integration",
    "issuetype": {
      "name": "Task"
    }
  }
}

The equivalent curl command to use would be:

curl -u username:password -X POST --data @collector.json -H "Content-Type: application/json" http://jira.mycompany.com:8091/rest/api/2/issue/

When I run the PHP script, there is no output displayed on the screen. I’m not seeing any error messages or indications of success, just a blank screen. What could be the reason for this lack of feedback, and how can I debug the connection?

You’re not actually sending a POST request even though you set up the data. Skip CURLOPT_POST and use CURLOPT_CUSTOMREQUEST instead - it works way better with REST APIs. Had the same headache when we moved to Jira Cloud.

Try this:

curl_setopt($curlSession, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curlSession, CURLOPT_USERPWD, "username:password");

Add some debugging before json_decode to see what Jira’s actually sending back:

var_dump($apiResponse);
echo "Length: " . strlen($apiResponse);

Jira loves returning HTML error pages instead of JSON when auth fails, which breaks json_decode without warning. The var_dump shows exactly what’s coming back - empty response, HTML garbage, or actual JSON errors.

You’re missing the POST method. Add curl_setopt($curlSession, CURLOPT_POST, true); - without it, it defaults to GET and won’t create issues. Also check if curl_exec returns anything before decoding since it returns false when it fails.

That blank screen means your script’s failing silently. You’re missing authentication and the POST method like others said, but you also need error handling to see what’s breaking. Throw this after curl_exec to catch what’s happening:

if ($apiResponse === false) {
    echo 'Curl failed: ' . curl_error($curlSession);
}
$httpCode = curl_getinfo($curlSession, CURLINFO_HTTP_CODE);
echo "HTTP Code: $httpCode\n";
echo "Response: $apiResponse\n";

I’ve hit this same problem when Jira needs specific headers or there’s network issues. The HTTP code will show you if it’s auth problems (401), bad request (400), or server errors (5xx). Also double-check your Jira URL works and the endpoint accepts whatever issue type you’re creating.

Your code’s missing authentication credentials. You set CURLOPT_HTTPAUTH to CURLAUTH_BASIC but didn’t provide the username and password.

Add this after curl_init:

curl_setopt($curlSession, CURLOPT_USERPWD, "username:password");

And throw in some error checking:

if (curl_error($curlSession)) {
    echo 'Curl error: ' . curl_error($curlSession);
}
echo "HTTP Status: " . curl_getinfo($curlSession, CURLINFO_HTTP_CODE);

Honestly though, dealing with all these curl options and API quirks gets old fast. Been there multiple times.

I just use Latenode for Jira integrations now. Drag and drop a Jira node, authenticate once, and it handles all the REST API mess. No more debugging curl sessions or managing auth tokens.

Built a workflow that creates Jira issues from various triggers in 5 minutes. Way cleaner than maintaining PHP scripts that break every time Jira updates.

Check it out: https://latenode.com