Post Multiple Files as Attachments Using the Confluence Rest API with PHP curl

Coline Macorol April 2, 2020

Hello All,

I posted a similar question regarding how to post a single file using the Confluence REST API with php curl. I was able to get that to work with this code: 

$ch = curl_init();

$postFile = __DIR__ . '\afile.txt';
$cFile = curl_file_create($postFile);

$postData = array('file' => $cFile);
$headers = array();
$headers[] = "X-Atlassian-Token: no-check";

curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/31391749/child/attachment");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username" . ":" . "password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

curl_exec($ch);
curl_close($ch);


Now I'm trying to post multiple files at once and have been unsuccessful in my attempts thus far, This is my most recent attempt:

$ch = curl_init();

$postFiles = array(__DIR__ . '\afile.txt', __DIR__ . '\bfile.txt',);
$postData = array();

foreach($postFiles as $index => $postFile) {
$cFile = curl_file_create($postFile);
$postData[$index] = $cFile;
}

$postData = array('file' => $cFile);
$headers = array();
$headers[] = "X-Atlassian-Token: no-check";

curl_setopt($ch, CURLOPT_URL, "http://localhost:8090/rest/api/content/31391749/child/attachment");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
curl_setopt($ch, CURLOPT_USERPWD, "username" . ":" . "password");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

curl_exec($ch);
curl_close($ch);

The code above only posts the bfile.txt file as an attachment.

I've been able to post multiple files using cURL, but I need to do it using php curl. Any ideas or suggestions on how to amend the code above to enable the posting of multiple files? Thanks in advance!

 

*NOTE: this post was somehow duplicated. I tried to find a way to delete one, but it seems that's not possible at the moment.*

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events