1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to retrieve user avatar URL?

Discussion in 'XenForo Development Discussions' started by def, Jun 22, 2015.

  1. def

    def Member

    Hey!

    In our secondary app we use xenForo users data for authentication. We achieve it via JSON, heres an example what it returns:

    Code:
    {"user_id":12834,"username":"tester2","email":"g4013185@trbvm.com","gender":"","custom_title":"","language_id":1,"style_id":0,"timezone":"Europe\/London","visible":1,"activity_visible":1,"user_group_id":2,"secondary_group_ids":"","display_style_group_id":2,"permission_combination_id":2,"message_count":0,"conversations_unread":0,"register_date":1432378577,"last_activity":1432378971,"trophy_points":0,"alerts_unread":0,"avatar_date":0,"avatar_width":0,"avatar_height":0,"gravatar":"","user_state":"email_confirm","is_moderator":0,"is_admin":0,"is_banned":0,"like_count":0,"warning_points":0,"is_staff":0,"br_profile_image":"","br_cropy":"0.00","authorized":0}

    It's all and even more data that we need, apart from user avatar URL. Can you help me modify the code, so it retrieves the avatar URL or any information that I could use to get to it? I'm pretty sure we use the 1.4. Thank you :)

    The code:
    PHP:
    <?php

    $startTime 
    microtime(true);
    $fileDir dirname(__FILE__);

    //XenForo init: read configuration
    require('/foo/bar/library/XenForo/Autoloader.php');

    //XenForo init: Get Instance
    XenForo_Autoloader::getInstance()->setupAutoloader('/foo/bar/library');

    //XenForo init: Initialize
    XenForo_Application::initialize('foo/bar/library/'$fileDir);

    XenForo_Application::set('page_start_time'$startTime);

    $username $_GET['username'];
    $password $_GET['password'];

    $db XenForo_Application::getDb();

    $data $db->fetchOne('
        SELECT
            auth.data
        FROM xf_user_authenticate AS auth
        INNER JOIN xf_user AS user ON
            (user.user_id = auth.user_id)
        WHERE user.username = ?
    '
    $username);

    $auth XenForo_Authentication_Abstract::createDefault();

    $auth->setData($data);

    $check $auth->authenticate($username$password);
    if (
    $check) {
       
    $data1 $db->fetchRow("SELECT * FROM xf_user WHERE username = ?"$username);
       
    $data1[authorized] = 1;
       echo 
    json_encode($data1);
    } else {
       
    $data1 $db->fetchRow("SELECT * FROM xf_user WHERE username = ?"$username);
       
    $data1[authorized] = 0;
       echo 
    json_encode($data1);
    }
    ?>
     
  2. Chris D

    Chris D XenForo Developer Staff Member

    Once you have the user record from xf_user containing at least the user_id, gender, avatar_date, gravatar, you can get the avatar URL like this:

    PHP:
    $user['avatarUrl'] = XenForo_Template_Helper_Core::callHelper('avatar', array($user's'));
    Where 's' is one of 's', 'm', or 'l' (small, medium or large size)
     
    jeffwidman likes this.
  3. def

    def Member


    Thank you very much, this is what I was looking for :) BTW, "large size" has original file proportions, is it intended?
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    Yes, that's intended.
     

Share This Page