0

I'm working on attaching files via PHP to confluence (version 5.9.10)

Here is my code

$ch=curl_init();
$headers = array(
    'X-Atlassian-Token: no-check'
);
$data = array('file' => '@test.txt');
curl_setopt_array(
    $ch,
    array(
        CURLOPT_URL=>'https://<path_to_confluence>/rest/api/content/<page_id>/child/attachment',
        CURLOPT_POST=>true,
        CURLOPT_VERBOSE=>1,
        CURLOPT_POSTFIELDS=>$data,
        CURLOPT_SSL_VERIFYHOST=> 0,
        CURLOPT_SSL_VERIFYPEER=> 0,
        CURLOPT_RETURNTRANSFER=>true,
        CURLOPT_HEADER=>false,
        CURLOPT_HTTPHEADER=> $headers,
        CURLOPT_USERPWD=>C_USERNAME.":".C_PASSWORD
    )
);
$result=curl_exec($ch);
$ch_error = curl_error($ch);
if ($ch_error) {
    echo "cURL Error: $ch_error";
} else {
    var_dump($result);
}
curl_close($ch);

But after running script I've next error: >

HTTP/1.1 500 Internal Server Error
Server: nginx/1.5.12
Date: Thu, 03 Nov 2016 10:12:44 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-ASEN: SEN-2160053
Set-Cookie: JSESSIONID=EE3116DFC552C7D4571608BFCF410559; Path=/; HttpOnly
X-Seraph-LoginReason: OK
X-AUSERNAME: user
Cache-Control: no-cache, must-revalidate
Expires: Thu, 01 Jan 1970 00:00:00 GMT
X-Content-Type-Options: nosniff
HTTP error before end of send, stop sending
Closing connection 0 string(93) "statusCode":500,"message":"java.lang.IllegalArgumentException:File name must not be null"

What I'm doing wrong? What have I missed?

2 Answers2

0

Try this:

$data = array('file' => '@' . realpath('test.txt'));
...
CURLOPT_POSTFIELDS=>$data

If the filename is required, you can try this instead:

$data = array('file' => '@' . realpath('test.txt') . ';filename=test.txt'));
...
CURLOPT_POSTFIELDS=>$data
mtheriault
  • 1,065
  • 9
  • 21
0

I encountered this error when I was using the lib cURL and was doing it the old way, like:

CURLOPT_POSTFIELDS => [
        'file' => '@/pathToFile'

]

But since it's deprecated from PHP 5.5, you need to use it the new way:

Imperative way:

CURLOPT_POSTFIELDS => [
        'file' => curl_file_create($filePath)

]

Object way:

CURLOPT_POSTFIELDS => [
        'file' => new CurlFile($filePath)

]

Tazrof
  • 11
  • 2