Adding attachment to Jira's api

时光总嘲笑我的痴心妄想 提交于 2019-12-10 15:19:40

问题


I am trying to attach a file to a Jira case, using their API. I am doing this in Drupal 6 (PHP v.5.0). Here is the code I have:

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

The problem is that the $result comes out as false, and the $ch_error states that it couldn't open the file. Does this error have something to do with Drupal or something to do with how I'm sending my request to Jira? BTW, if I use an absolute path, though, like this:

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

The upload works just fine.


回答1:


This should work for you:

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

This was a bug with cURL PHP <5.2.10, you can see this in the PHP bug tracker: https://bugs.php.net/bug.php?id=48962 (And in the comments you see, that it has been fixed)

And if you use PHP 5.5.0+ you can use this:

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

You can also see this in a JIRA comment: JIRA Comment

Also i think you want to change this:

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

to this:

curl_setopt($ch, CURLOPT_HTTPHEADER, $header);


来源:https://stackoverflow.com/questions/28030564/adding-attachment-to-jiras-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!