# Upload avatar api with php

**URL:** https://meta.discourse.org/t/upload-avatar-api-with-php/108218
**Category:** Development
**Created:** [February 2, 2019, 6:12pm UTC](https://meta.discourse.org/t/upload-avatar-api-with-php/108218 "2019-02-02T18:12:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Jeremy\_R](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jeremy_r/32/130216_2.png) [@Jeremy\_R](https://meta.discourse.org/u/Jeremy_R)
#### Post date: [February 2, 2019, 6:12pm UTC](https://meta.discourse.org/t/upload-avatar-api-with-php/108218/1 "2019-02-02T18:12:08Z")

</div>

Hello,

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

Here is my code :

```php
$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 🙂

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 2, 2019, 8:52pm UTC](https://meta.discourse.org/t/upload-avatar-api-with-php/108218/2 "2019-02-02T20:52:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jeremy\_R](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jeremy_r/32/130216_2.png) [@Jeremy\_R](https://meta.discourse.org/u/Jeremy_R)
#### Post date: [February 2, 2019, 10:00pm UTC](https://meta.discourse.org/t/upload-avatar-api-with-php/108218/3 "2019-02-02T22:00:54Z")

</div>

Hello Jeff,

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

> **[Screenshot](https://prnt.sc/mftilo)**
>
> Captured with Lightshot

Thank you 🙂

---

<div class="post-metadata">

### Author: ![codinghorror](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/codinghorror/32/110067_2.png) [@codinghorror](https://meta.discourse.org/u/codinghorror)
#### Post date: [February 3, 2019, 10:27am UTC](https://meta.discourse.org/t/upload-avatar-api-with-php/108218/4 "2019-02-03T10:27:30Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Jeremy\_R](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jeremy_r/32/130216_2.png) [@Jeremy\_R](https://meta.discourse.org/u/Jeremy_R)
#### Post date: [February 3, 2019, 10:46am UTC](https://meta.discourse.org/t/upload-avatar-api-with-php/108218/5 "2019-02-03T10:46:33Z")

</div>

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.
