How to correctly initialise XenForo environment and create a user?

Dale

New Member
#1
I am trying to create a new XenForo user in PHP.

I am using this example code:
Mã:
$newusername = "Joseph";
$newpassword = "12345";
$newemail = "joseph@yahoo.com";

$fileDir = "/home/myfullpath/public_html/members/xenforo";


require( $fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

$startTime = microtime(true);
XenForo_Autoloader::initialize($fileDir . '/library', $fileDir);
XenForo_Autoloader::set('page_start_time', $startTime);
// XenForo_Application::disablePhpErrorHandler();

// create new user
$writer = XenForo_DataWriter::create('XenForo_DataWriter_User');

// set all the values 
$writer->set('username', $newusername);
$writer->set('email', $newemail);
$writer->setPassword($newpassword, $newpassword);
$writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);

// save user
$result = $writer->save();
echo $result;
The problem is, I get this error:
Mã:
Fatal error: Call to undefined method XenForo_Autoloader::initialize() in /home/myfullpath/public_html/members/signup/create-user.php on line 13
Line 13 is:
Mã:
XenForo_Autoloader::initialize($fileDir . '/library', $fileDir);
I checked the Autoloader.php file and couldn't find the initialize method so I don't doubt what PHP is telling me.

How are you meant to load the XenForo environment so you can create a new user? Did they update it in a recent version update or something?
 

Admin

Administrator
Thành viên BQT
#2
I found the problem with the code...

I should be targeting the XenForo_Application method not XenForo_Autoloader...

Mã:
require( $fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');   

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);
XenForo_Application::setDebugMode(true);
XenForo_Application::disablePhpErrorHandler();
 
Top