Как мы можем прикрепить несколько изображений с помощью api JIRA rest attachments через PHP Curl? - PullRequest
0 голосов
/ 24 мая 2018

Я могу прикрепить одно изображение через Jira Rest Api, но мне не удается отправить несколько изображений через него.Это мой код для одного вложения.Нужна помощь, чтобы заставить работать несколько вложений.

Ссылка: Файл прикрепления Jira для выпуска с PHP и CURL

$cfile = new CURLFile($attachment['tmp_name'],$attachment['type'], $attachment['name']);
$data = array('file' => $cfile);

$url = "{$uriapi}"."issue/"."{$bugid}"."/attachments";
curl_setopt_array(
$ch,
array(
CURLOPT_URL=>$url,
CURLOPT_POST=>true,
CURLOPT_VERBOSE=>1,
CURLOPT_POSTFIELDS=>$data,
CURLOPT_INFILESIZE => 10,
CURLOPT_SSL_VERIFYHOST=> 0,
CURLOPT_SSL_VERIFYPEER=> 0,
CURLOPT_RETURNTRANSFER=>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";
return "Error Opening file. Failed to add Attachment";
}
elseif(isset($result)){
//return "Attachment added";
return "";
}
else{
return "Failed to add Attachment";
}
curl_close($ch);
}

1 Ответ

0 голосов
/ 09 ноября 2018

Следующий код отлично работает для меня, надеюсь, это поможет.

$username = "xxxxx";
$password = "xxxxx";

$url = "https://YourUrl/rest/api/latest/issue/YourKey/attachments";

$attachments = array("attachment1", "attachment2", "attachment3");
$curl = curl_init();
for ($i = 0; $i < count($attachments); $i++) {
$attachmentPath = "/your/attachment/path/$attachments[$i]";
$filename = array_pop(explode('/', $attachmentPath));

$cfile = new CURLFile($attachmentPath);
$cfile->setPostFilename($filename);

$data = array('file' => $cfile);

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

curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

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

if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    echo $result;
}
}
curl_close($curl);
...