Adding an application to Jira via API

I am using the Jira API to add an attachment file to the case. My problem is that my code is attaching a file, and I go to the JIRA case for confirmation, I see two things. Firstly, if this is an image, I see a thumbnail of the image. However, if I click on it, I get an error: "The requested content could not be downloaded. Please try again." Secondly, under the thumbnail, instead of showing the file name, it has the path from which the file was originally downloaded (id: c: / wamp / www / .... "Is there a reason why this happens? Mine the code:

$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:
//c:/wamp/www/mySite/web/system/files/my_folder/DSC_0344_3.JPG

$data = array('file'=>"@". $attachmentPath, 'filename'=>'DSC_0344_3.JPG');
$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, $header);
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);

Jira, jira, , : c:/wamp/www/mySite/web/system/files/my_folder/DSC_0344_3.JPG .

+4
2

:

$data = array('file'=>"@". $attachmentPath . ';filename=DSC_0344_3.JPG');

PHP cURL < 5.5.0, > 5.2.10, . JIRA API

PHP >= 5.5.0 CURLFile, .

$cfile = new CURLFile($attachmentPath);
$cfile->setPostFilename('DSC_0344_3.JPG');
$data = array('file'=>$cfile);
+7

- : , , php 7

function attachFileToIssue($issueURL, $attachmentURL) {
// issueURL will be something like this: http://{yourdomainforjira}.com/rest/api/2/issue/{key}/attachments 
// $attachmentURL will be real path to file (i.e. C:\hereswheremyfilelives\fileName.jpg) NOTE: Local paths ("./fileName.jpg") does not work!

$ch = curl_init();
$headers = array(
    'X-Atlassian-Token: nocheck',
    'Content-type: multipart/form-data'
);

$cfile = new CURLFile($attachmentURL);
$cfile->setPostFilename(basename($attachmentURL));
$data = array("file" => $cfile);
curl_setopt_array(
    $ch,
    array(
        CURLOPT_URL => $issueURL,
        CURLOPT_VERBOSE => 1,
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_USERPWD => "{username}:{password}"
    )
);
$result = curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error)
    echo "cURL Error: " . $ch_error;

curl_close($ch);

}

0

All Articles