# 使用 PHP 上传头像 API

**URL:** <https://meta.discourse.org/t/upload-avatar-api-with-php/108218>\
**Category:** Development\
**Created:** [2019 年2 月 2 日 18:12 UTC](https://meta.discourse.org/t/upload-avatar-api-with-php/108218 "2019-02-02T18:12:08Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

作者： ![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)\
发布日期： [2019 年2 月 2 日 18:12 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 🙂

---

_[View the full topic](https://meta.discourse.org/t/upload-avatar-api-with-php/108218)._
