Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Create Issue with REST API and CURL using PHP

Christoph Huber September 1, 2013

Hi

Could pls someone post an example code, how to create an issue with the rest api? i dont get it.

i am always getting a php error 415.

I think i am doing smth wrong^^

this would be my code so far:

<?php

$handle=curl_init();
$headers = array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Basic YWRtaW46eWFoYWxh'
);

$data = <<<JSON
{
"fields": {
"project":
{
"key": "SYNC"
},
"summary": "No REST for the Wicked.",
"description": "Creating of an issue using ids for projects and issue types using the REST API",

}
}
JSON;



curl_setopt_array(
$handle,
array(
CURLOPT_URL=>'baseurl/jira/rest/api/2/issue/',
CURLOPT_POST=>true,
//CURLOPT_HTTPGET =>true,

CURLOPT_VERBOSE=>1,
CURLOPT_POSTFIELDS=>$data,
CURLOPT_SSL_VERIFYHOST=> 0,
CURLOPT_SSL_VERIFYPEER=> 0,
CURLOPT_RETURNTRANSFER=>true,
CURL_HEADER=>false,
CURLOPT_HTTPHEADER=> $headers,
CURLOPT_USERPWD=>"user:pwd",
//CURLOPT_CUSTOMREQUEST=>"POST"
)

);
$result=curl_exec($handle);
$ch_error = curl_error($handle);

if ($ch_error) {
echo "cURL Error: $ch_error";
} else {
echo $result;
}

curl_close($handle);


?>

i hope someone can help me ;)

kind regards

chris

7 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

6 votes
Answer accepted
Christoph Huber September 2, 2013

it works fine! Thank you man!

her is now my code, if someone else would like to create issues ;)

&lt;?php
 
$username = 'xxxxx';
$password = 'xxxxx';
              
$url = "xxxx/rest/api/2/issue/";

$data = array(

'fields' =&gt; array(

'project' =&gt; array(

'key' =&gt; 'xxx',

),

'summary' =&gt; 'This is summary',

'description' =&gt; 'This is description',
"issuetype" =&gt; array(

    "self" =&gt; "xxxx",
    "id" =&gt; "xxxx",
    "description" =&gt; "xxxxx",
    "iconUrl" =&gt; "xxxxx",
    "name" =&gt; "xxxx",
    "subtask" =&gt; false

),
),

);

$ch = curl_init();
 
$headers = array(
 
    'Accept: application/json',
 
    'Content-Type: application/json'
 
);
 
  
 
$test = "This is the content of the custom field.";
 
  
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
curl_setopt($ch, CURLOPT_VERBOSE, 1);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
 
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
 
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
 
curl_setopt($ch, CURLOPT_URL, $url);
 
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
 
  
 
$result = curl_exec($ch);
 
$ch_error = curl_error($ch);
 
  
 
if ($ch_error) {
 
    echo "cURL Error: $ch_error";
 
} else {
 
    echo $result;
 
}
 
  
 
curl_close($ch);
 
  
 
?&gt;

OmarA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 2, 2013

No worries mate, please accept your answer so it can benefit others :)

2 votes
OmarA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 2, 2013

I have created this PHP code somewhile ago to fetch some projects from JIRA, you may change the URL and the request type to add new issues :

&lt;?php

 

$username = 'test';

$password = 'test';

 

$url = "https://xxxxx.xxxxxxx.net/rest/api/2/project";

$ch = curl_init();

 

$headers = array(

    'Accept: application/json',

    'Content-Type: application/json'

);

 

$test = "This is the content of the custom field.";

 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

//curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_setopt($ch, CURLOPT_URL, $url);

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

 

$result = curl_exec($ch);

$ch_error = curl_error($ch);

 

if ($ch_error) {

    echo "cURL Error: $ch_error";

} else {

    echo $result;

}

 

curl_close($ch);

 

?&gt;

So if you want to re-use it for creating issues, you will need to change the REST URL to /rest/api/2/issue/ and use "POST" instead of "GET"

Meghambari Khavnekar September 4, 2013

Thanks! that was very helpful and precise!

0 votes
KlausS November 26, 2013

HI Christoph! I am facing the same problem, i always receive an 415 error, what did you do to solve that problem? Could you help me out? Best regards!

0 votes
Christoph Huber September 2, 2013

noone has an answere for me :( ?

0 votes
Christoph Huber September 1, 2013

when i am using this code for example i am getting this error :

{"errorMessages":["Invalid numeric value: Missing integer part (next char '-' (code 45))\n at [Source: org.apache.catalina.connector.CoyoteInputStream@61c72476; line: 1, column: 3]"]}

&lt;?php

$data = array(

'fields' =&gt; array(

'project' =&gt; array(

'key' =&gt; 'TEST',

),

'summary' =&gt; 'This is summary',

'description' =&gt; 'This is description',

),

);


$username = '***';
$password = '***';

$url = '***rest/api/2/issue/';

$curl = curl_init();
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

$issue_list = (curl_exec($curl));
echo hi . $issue_list;

?&gt;

Dorjoo Khishigee (Ds3c) January 16, 2018
sa

0 votes
Christoph Huber September 1, 2013

unfortunately it has not solved my problem...

it would be greate, if someone has an example how to create an issue with php using curl

i tried some different examples, which i found on the internet but none of them worked correctly :/

0 votes
Teck-En
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
September 1, 2013

I'm not good with php, but found this post in stackoverflow which similar to the way of your codings: http://stackoverflow.com/a/11091261

You might want to try the way you specified the headers and see how it goes?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events