【问题标题】:Adding attachment to Jira's api将附件添加到 Jira 的 api
【发布时间】:2015-03-17 19:24:44
【问题描述】:

我正在尝试使用他们的 API 将文件附加到 Jira 案例。我在 Drupal 6 (PHP v.5.0) 中这样做。这是我的代码:

$ch = curl_init();
$header = array(
  'Content-Type: multipart/form-data',
   'X-Atlassian-Token: no-check'
);
$attachmentPath = $this->get_file_uploads();
//$attachmentPath comes out to be something like:
//http://localhost/mySite/web/system/files/my_folder/DSC_0344_3.JPG

$data = array('file'=>"@". $attachmentPath, 'filename'=>'test.png');
$url= 'https://mysite.atlassian.net/rest/api/2/issue/20612/attachments/';

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, $this->get_jira_headers());
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,  CURLOPT_POSTFIELDS ,$data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");

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

问题在于 $result 显示为 false,而 $ch_error 表明它无法打开文件。此错误是否与 Drupal 或与我如何向 Jira 发送请求有关?顺便说一句,如果我使用绝对路径,就像这样:

$attachmentPath = 'C:\wamp\www\mySite\web\sites\mySite.net\files\my_folder\DSC_0333.JPG';

上传工作正常。

【问题讨论】:

    标签: php drupal drupal-6 jira jira-rest-api


    【解决方案1】:

    这应该适合你:

    $data = array('file'=>"@". $attachmentPath . ';filename=test.png');
    

    这是一个 cURL PHP https://bugs.php.net/bug.php?id=48962(在您看到的 cmets 中,它已被修复)

    如果你使用 PHP 5.5.0+,你可以使用这个:

    $cfile = new CURLFile($attachmentPath);
    $cfile->setPostFilename('test.png');
    $data = array('file'=>$cfile);
    

    您也可以在 JIRA 评论中看到这一点:JIRA Comment

    我也认为你想改变这个:

    curl_setopt($ch, CURLOPT_HTTPHEADER, $this->get_jira_headers());
    

    到这里:

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    

    【讨论】:

    • 谢谢。我实际上不小心将赏金添加到了这个问题,同时打算将它添加到我添加的另一个问题中。但是,交易就是交易,我很高兴它有效!
    • @jason 不客气!是的,我认为这就是让它发挥作用的主要目标!
    • 它返回 cURL 错误:格式错误
    猜你喜欢
    • 2015-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2012-08-06
    • 1970-01-01
    相关资源
    最近更新 更多