POST /users/:userId/avatar ?

Discussion in '[bd] API' started by RS13, Sep 19, 2016.

  1. RS13

    RS13 New Member

    #1 , Sep 19, 2016
    Last edited: Sep 19, 2016
    Hello,

    I've installed the API and I'm writing a client-side library based on the php-demo code to run on my CodeIgniter and Laravel apps.

    So far, all of the GET requests are working great.

    However, I want to use the POST /users/:userId/avatar code to transfer my users' avatars from my former phpBB forum to my new Xenforo forum.

    I'm not having much luck getting this to work, and I was hoping for some help or example code. After a fair bit of googling, I have come up with this:

    The correct code is posted below.

    I'm using the "read admincp post" scope. I'm specifically confused about this clause from the API documentation:

    Parameters: avatar (required): binary data of the avatar.

    I'm getting this response:

    {"errors":["This request requires file upload `avatar`."],"system_info":{"visitor_id":3,"time":1474265364}}

    I'm using PHP 7.0 with nginx 1.10.

    Any help would be much appreciated. Thanks.
     
  2. xfrocks

    xfrocks XenForo rocks!
    Staff Member

    This line should be:

    Code:
    $data = ['avatar' => '@'.$filename];
    And the `$filename` should be a valid path, best to use an absolute path.
     
  3. RS13

    RS13 New Member

    I got it!

    For PHP 7.0, this worked for me:

    $filename = '/Users/myUsername/Sites/mySite.com/backup/avatars/avatar2.png';

    if (! file_exists($filename))
    die('File does not exist');

    $requestUrl = 'users/3/avatar';

    $requestUrl = sprintf(
    '%s/index.php?%s&oauth_token=%s',
    $this->config['api_root'],
    $requestUrl,
    rawurlencode($this->accessToken)
    );


    $ch = curl_init();

    $data = ['avatar' => curl_file_create($filename, 'image/png', 'myAvatar.png')];

    $options = [
    CURLOPT_URL => $requestUrl,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $data,
    ];

    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    curl_close($ch);
     
    xfrocks likes this.
Loading...