本文介绍了Jira附加文件将与PHP和CURL一起发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我目前的尝试:

 $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与该文件位于同一目录中.我已经在托管此服务器的Web服务器上安装了curl,可以在jira中创建问题,只是似乎无法上传附件...

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?

添加赏金,这是我尝试过的一些事情:

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

  1. 尝试无检查和无检查
  2. 同时尝试@ testing.txt和testing.txt
  3. 删除内容类型:多部分/表单数据"
  4. 完整路径,例如:$data = array('file'=>"@C:\xampp\htdocs\Website\testing.txt ,'filename'=>'testing.txt');
  5. 由于已知的卷曲错误,也尝试过这样的方法:$data = array('file'=>"@C:\xampp\htdocs\Website\testing.txt" ';filename=testing.txt');
  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');

上述所有组合.不管我尝试什么,都行不通.还确保了我是Jira中的管理员级别的用户.我觉得我的代码应该可以工作...但是显然不能.

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.

推荐答案

我最初的假设是错误的:它同时适用于 no-check nocheck -它没有没关系.

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

首先要创建一个curl文件对象,而不是将文件名作为file的参数:

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');

然后将其放入数组:

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

这篇关于Jira附加文件将与PHP和CURL一起发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:41