Reputation: 147
I'm trying to create issue using jira rest api, here's my code:
$new_issue = array(
'fields' => array(
'project' => array('key' => "KEY"),
'summary' => $this->Summary,
'description' => $this->Description,
'issuetype' => array('name' => 'Bug')
)
);
$body = json_encode($new_issue);
self::$handle = curl_init();
curl_setopt(self::$handle, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt(self::$handle, CURLOPT_POSTFIELDS, $body);
curl_setopt_array(self::$handle, array(
CURLOPT_URL => "jiraUrl//rest/api/2/issue/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_HTTPHEADER => array("content-type:application/json"),
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_ENCODING => ''
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $username . ':' . $password
));
$response = curl_exec(self::$handle);
$error = curl_error(self::$handle);
I'm getting this error: {"errorMessages":["No content to map to Object due to end of input"]} any suggestions?
Upvotes: 1
Views: 2301
Reputation: 18873
Update for 2021, you can use this PHP Jira REST client: https://github.com/lesstif/php-jira-rest-client
<?php
require 'vendor/autoload.php';
use JiraRestApi\Issue\IssueService;
use JiraRestApi\Issue\IssueField;
use JiraRestApi\JiraException;
try {
$issueField = new IssueField();
$issueField->setProjectKey("TEST")
->setSummary("something's wrong")
->setAssigneeName("lesstif")
->setPriorityName("Critical")
->setIssueType("Bug")
->setDescription("Full description for issue")
->addVersion(["1.0.1", "1.0.3"])
->addComponents(['Component-1', 'Component-2'])
// set issue security if you need.
->setSecurityId(10001 /* security scheme id */)
->setDueDate('2019-06-19')
;
$issueService = new IssueService();
$ret = $issueService->create($issueField);
//If success, Returns a link to the created issue.
var_dump($ret);
} catch (JiraRestApi\JiraException $e) {
print("Error Occured! " . $e->getMessage());
}
And link to Jira docs
Upvotes: 1
Reputation: 454
For anyone still wondering how to create an issue with JIRA REST API the below minimal code is working for me:
$url = "http://your.domain.here/rest/api/latest/issue/"
$username = "username";
$password = "password";
$txt = '{
"fields": {
"project": {
"key": "KEY"
},
"summary": "SUMMARY",
"description": "DESCRIPTION",
"issuetype": {
"name": "ISSUETYPE"
}
}
}';
// Create a new cURL resource
$ch = curl_init ();
// Set URL and other appropriate options
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $txt );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_USERPWD, $username . ":" . $password );
$headers = array ();
$headers [] = "Content-Type: application/json";
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
// Grab URL and pass it to the browser
$result = curl_exec ( $ch );
echo $result;
Upvotes: 0
Reputation: 4265
Missing closing quotes here:
CURLOPT_URL => "jiraUrl//rest/api/2/issue/
and jiraUrl
probably needs to be like $jiraUrl
.
So, my take:
CURLOPT_URL => "$jiraUrl//rest/api/2/issue/",
Upvotes: 0