Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

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

Coline Macorol April 2, 2020 edited

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
atlassian, confluence, marketing templates, confluence templates, marketing project management, content strategy, campaign planning, marketing collaboration, confluence for marketers, atlassian learning, marketing workflows, confluence best practices

5 must-use templates for Marketing teams (from a Confluence Marketer!) 👩🏻‍🎨

Who doesn’t love a good template? We sure do! Check out our top template picks for Marketing teams who want to streamline their processes, enhance collaboration, and take their marketing to new heights.

Read more 📚
AUG Leaders

Atlassian Community Events