Access to user custom fields from external php file in joomla 4.4

52 Views Asked by At

In joomla 4.4 I have an ajax request to an external php file. I want to access to user custom fields. This is my code:

  <?php
  header('Access-Control-Allow-Origin: *');
  define('_JEXEC', 1);    

  define('JPATH_BASE', realpath(dirname(__FILE__) . '/../..'));
  require_once JPATH_BASE . '/includes/defines.php';
  require_once JPATH_BASE . '/includes/framework.php';

 $container = \Joomla\CMS\Factory::getContainer();

 $container->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');


 $app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
 \Joomla\CMS\Factory::$application = $app;


   use Joomla\CMS\Factory;
   use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

  if(isset($_POST['username']) && is_string($_POST['username']))
  {

    $username = $_POST['username'];
    $db = Factory::getDbo();

    $query = $db->getQuery(true);

    $query
      ->select('*')
      ->from($db->quoteName('#__users'))
      ->where($db->quoteName('username') . ' = ' . $db->quote($username))
      ->setLimit('1');

    $db->setQuery($query);

    $user = $db->loadObject();

    if($user){
      $user = Factory::getUser($user->id);

      $customFields = FieldsHelper::getFields('com_users.user', $user, true);
      
      echo $customFields[0]->value;

    }
    else echo "-1";

}else{
    echo('location:index.php');
}

 ?>

But in line: $customFields = FieldsHelper::getFields('com_users.user', $user, true); it gets error 500.

Please help me.

0

There are 0 best solutions below