简体   繁体   English

Jira附加文件将与PHP和CURL一起发布

[英]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. 我找到了几个有关如何在jira中上载问题附件的示例,但是我无法使其中任何一个起作用。 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. 我在Jira社区帮助论坛上发布了这个问题,但是已经有一个多星期,有0条回复,因此希望这里的社区能够为您提供帮助。

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. testing.txt与该文件位于同一目录中。 I have curl installed on the webserver where this is hosted, can create issues in jira fine, just cant seem to upload attahcments... 我已经在托管此的网络服务器上安装了curl,可以在jira中创建问题,只是似乎无法上传附件...

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 同时尝试@ testing.txt和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'); 这样的完整路径: $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'); 由于已知的卷曲错误,也尝试过这种方法: $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. 还确保了我是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. 我最初的假设是错误的:它的作品既NOCHECKNOCHECK -没关系。

Instead of putting a file name as a parameter for file , you have first create a curl file object like this: 而不是把文件名作为参数的file ,你首先要创建一个这样的卷发文件对象:

$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);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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