Upload avatar api with php

Hello,

I have a problem with the api to upload avatars.
I have a 500 error.

Here is my code :

$files = [
  realpath('logo.png')
];

$postData = array(
  "api_key"      => API_KEY,
  "api_username" => API_USERNAME,
  "type"         => "avatar",
  "user_id"      => $user_id,
  "synchronous"  => true
);

foreach ($files as $index => $file) {
  $postData['files[' . $index . ']'] = curl_file_create(
    realpath($file),
    mime_content_type($file),
    basename($file)
  );
}
print_r($postData);
$handler   = curl_init();
$headers[] = "Content-Type: multipart/form-data;";
curl_setopt( $handler, CURLOPT_URL, "https://forum.xxx.fr/uploads.json" );
curl_setopt( $handler, CURLOPT_POSTFIELDS, http_build_query( $postData ) );
curl_setopt( $handler, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $handler, CURLOPT_POST, true );
curl_setopt( $handler, CURLOPT_SSL_VERIFYPEER, false );
$response = curl_exec( $handler );
if ($debug) {
  if ( $response !== false ) {
    var_dump( $response );
  } else {
    print "Could not get a response";
  }
}

Here is the answer :

Array
(
    [api_key] => xxxxx
    [api_username] => xx
    [type] => avatar
    [user_id] => 1
    [synchronous] => 1
    [files[0]] => CURLFile Object
        (
            [name] => /home/wacha/domains/domainname/public_html/logo.png
            [mime] => image/png
            [postname] => logo.png
        )

)
{"status":500,"error":"Internal Server Error"}bool(true)

Any idea why do i have that 500 error ?

Thank you :slight_smile:

Check the /logs on Discourse side in your web browser while logged in as admin.

Hello Jeff,

Thank you for your fast answer.
Here is a screen of my log :

Thank you :slight_smile:

Screenshot is not what is needed here. You’re a developer. You need to dig into those details.

yes, but i don’t know ruby that is why i’m asking help.
I thought the developper forum was the place to ask that kind of question.