添加附件到Jira的api

时间:2015-01-19 17:42:47

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

我正在尝试使用他们的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';

上传工作正常。

1 个答案:

答案 0 :(得分:3)

这应该适合你:

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

这是cURL PHP< 5.2.10的错误,您可以在PHP错误跟踪器中看到这一点:https://bugs.php.net/bug.php?id=48962(在您看到的评论中,它已被修复)

如果你使用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);