XF 2.1 File upload via API

developr

Active member
I want to add avatars to users via the xenforo api.

PHP:
function apiCall($action,$scope,$post,$upload=0) {
     $headers = array(
        'Content-type: application/x-www-form-urlencoded',
        'XF-Api-User: 1',
        'XF-Api-Key: '.APIKEY
     );
     if(!$upload) {
          $post = @http_build_query($post);
     }
     $url = 'https://127.0.0.1/api'.$scope;
     $ch = curl_init();
     if($upload) {
          $file = $post['avatar'];
          $mime = mime_content_type($file);
          $info = pathinfo($file);
          $name = $info['basename'];
          $output = new CURLFile($file, $mime, $name);
          $post['avatar'] = $output;
     }
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$action);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $json = curl_exec($ch);
     curl_close($ch);
     return json_decode($json);
}

The API will response with "success" but it doesn't work.

The API require "file" in postdata. But how? A link to the file doesn't work. A resource doesn't work. Without an error it's hard to find a solution.

 
Solution
Try and error... now here is the solution.

PHP:
function apiattach($action,$scope,$post) {
     $headers = array(
        'XF-Api-User: 1',
        'XF-Api-Key: '.APIKEY
     );
     $url = 'https://127.0.0.1/api'.$scope;
     $ch =...

developr

Active member
Try and error... now here is the solution.

PHP:
function apiattach($action,$scope,$post) {
     $headers = array(
        'XF-Api-User: 1',
        'XF-Api-Key: '.APIKEY
     );
     $url = 'https://127.0.0.1/api'.$scope;
     $ch = curl_init();
     $headers = array(
        'Content-type: multipart/form-data',
        'XF-Api-User: 1',
        'XF-Api-Key: '.APIKEY
     );
     $file = $post['avatarurl'];
     $mime = mime_content_type($file);
     $info = pathinfo($file);
     $name = $info['basename'];
     $file = new CURLFile($file, $mime, $name);
     $post['avatar'] = $file;
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$action);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $json = curl_exec($ch);
     curl_close($ch);
     return json_decode($json);
}
 
Solution
Try and error... now here is the solution.

PHP:
function apiattach($action,$scope,$post) {
     $headers = array(
        'XF-Api-User: 1',
        'XF-Api-Key: '.APIKEY
     );
     $url = 'https://127.0.0.1/api'.$scope;
     $ch = curl_init();
     $headers = array(
        'Content-type: multipart/form-data',
        'XF-Api-User: 1',
        'XF-Api-Key: '.APIKEY
     );
     $file = $post['avatarurl'];
     $mime = mime_content_type($file);
     $info = pathinfo($file);
     $name = $info['basename'];
     $file = new CURLFile($file, $mime, $name);
     $post['avatar'] = $file;
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST,$action);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     $json = curl_exec($ch);
     curl_close($ch);
     return json_decode($json);
}
Thank you!
 
Top