简体   繁体   中英

Jira attach file to issue with PHP and CURL

I have found several examples on how to upload an attachment to an issue in jira, however I have not been able to make any of them work. I posted this question on the Jira Community Help Forums but it has been over a week with 0 replies so hoping the community here can help.

Here is my current attempt:

 $Jirausername = 'myUsername';
    $Jirapassword = 'myPassword';

    $ch=curl_init();
$headers = array(
    'X-Atlassian-Token: nocheck',
    'Content-Type: multipart/form-data'
);
$data = array('file' => "testing.txt");


curl_setopt_array(
    $ch,
    array(
        CURLOPT_URL=>'https://myCompany.net/rest/api/latest/issue/TAG-78/attachments',
        CURLOPT_POST=>true,
        CURLOPT_VERBOSE=>1,
        CURLOPT_POSTFIELDS=>$data,
        CURLOPT_SSL_VERIFYHOST=> 0,
        CURLOPT_SSL_VERIFYPEER=> 0,
        CURLOPT_RETURNTRANSFER=>true,
        CURLOPT_HEADER=>false,
        CURLOPT_HTTPHEADER=> $headers,
        CURLOPT_USERPWD=>"$Jirausername:$Jirapassword"
    )
);
$result=curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    var_dump($result);
}
curl_close($ch);

testing.txt is in the same directory as this file. I have curl installed on the webserver where this is hosted, can create issues in jira fine, just cant seem to upload attahcments...

When I run this page it displays:

string(0) "" 

No attachment is uploaded as well needless to say. Any ideas what I am doing wrong?

EDIT: Adding a bounty, here are some things I have tried:

  1. trying both nocheck and no-check
  2. trying both @testing.txt and testing.txt
  3. removing 'Content-Type: multipart/form-data'
  4. Full paths as such: $data = array('file'=>"@C:\\xampp\\htdocs\\Website\\testing.txt ,'filename'=>'testing.txt');
  5. Tried like this too because of a known curl error: $data = array('file'=>"@C:\\xampp\\htdocs\\Website\\testing.txt" ';filename=testing.txt');

And every combination of those above. No matter what I try it does not work though. Have also ensured I am an admin level user in Jira. I feel like my code should work... but clearly not.

My initial assumption was wrong: it works with both no-check and nocheck -- it doesn't matter.

Instead of putting a file name as a parameter for file , you have first create a curl file object like this:

$cfile = curl_file_create('testing.txt');

And then put it into array:

$data = array('file' => $cfile);

Here is the full solution, which worked for me:

<?php
 $Jirausername = '<username>';
 $Jirapassword = '<password>';

$ch=curl_init();
$headers = array(
    'X-Atlassian-Token: nocheck',
    'Content-Type: multipart/form-data'
);


$cfile = curl_file_create('testing.txt');
$data = array('file' => $cfile);


curl_setopt_array(
    $ch,
    array(
        CURLOPT_URL=>'https://<JIRA-SERVER>/rest/api/latest/issue/<ISSUE-KEY>/attachments',
        CURLOPT_POST=>true,
        CURLOPT_VERBOSE=>1,
        CURLOPT_POSTFIELDS=>$data,
        CURLOPT_INFILESIZE => 5,
        CURLOPT_SSL_VERIFYHOST=> 0,
        CURLOPT_SSL_VERIFYPEER=> 0,
        CURLOPT_RETURNTRANSFER=>true,
        CURLOPT_HEADER=>true,
        CURLOPT_HTTPHEADER=> $headers,
        CURLOPT_USERPWD=>"$Jirausername:$Jirapassword"
    )
);
$result=curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    var_dump($result);
}
curl_close($ch);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM