XF 2.0 XenForo_DataWriter to create post in XF2

nolatron

Member
I have a php script that's being using the Xenforo_Datawriter in XF 1.5.6 to auto create posts.

After updating to Xenforo 2, the script no longer works but not sure why. Here's what the script currently uses.

Code:
$fileDir = "/home/nolatron/public_html/xxxxxxxxxxx/forums"; 
    require($fileDir.'/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
 
    $startTime = microtime(true);
    XenForo_Application::initialize($fileDir . '/library', $fileDir);
    XenForo_Application::set('page_start_time', $startTime);
    XenForo_Application::disablePhpErrorHandler();
    
$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
$writer->set('user_id', '2');
$writer->set('username', 'XXXXXXXXX');
$writer->set('node_id', '20');
$writer->set('titlexeno', ''.$fname.' '.$lname.'');
$writer->set('prefix_id', '1');

$postWriter = $writer->getFirstMessageDw();
$postWriter->set('message', 'POST CONTENT GOES HERE');
$writer->save();

It's not generating any errors that I can find, it just doesn't create the post. So not sure what's changed between versions.
 

nolatron

Member
Well, I've been doing some searching to try and make heads and tails of this stuff (I'm far from a programmer), and I pieced together this.

Code:
$dir = "/home/nolatron/public_html/xx.com/forums";
require($dir . '/src/XF.php');
XF::start($dir);
$forum = '20';
$title = 'Subject';
$message = 'Message Test';
$user = 'TheDepot';

\XF::asVisitor($user, function() use ($forum, $title, $message)
{
    $creator = \XF::service('XF:Thread\Creator', $forum);
    $creator->setContent($title, $message);
    $creator->setPrefix($forum['default_prefix_id']);
    $creator->setIsAutomated();
    $creator->save();
});

but results in this error:
  • ErrorException: [E_RECOVERABLE_ERROR] Argument 1 passed to XF::asVisitor() must be an instance of XF\Entity\User, integer given, called in /home/nolatron/public_html/xx.com/depotplus/apply/exec.php on line 302 and defined
  • src/XF.php:386
  • Generated by: Unknown account
  • Jan 10, 2018 at 8:11 PM
Stack trace
#0 src/XF.php(386): XF::handlePhpError(4096, 'Argument 1 pass...', '/home/nolatron/...', 386, Array)
#1 /home/nolAatron/public_html/xxx.com/depotplus/apply/exec.php(302): XF::asVisitor(2, Object(Closure))
#2 {main}

Am I at least heading in the right direction? :)

for the user I've tried the user id 2, the name TheDepot and even TheDepot.2 but all result in the same error.
 

DragonByte Tech

Well-known member
The user needs to be a User entity, you need to call something like $user = \XF::em()->find('XF:User', $userId); (untested).


Fillip
 

nolatron

Member
Thanks! That got me pointed in the right direction and I was able to get it working again. Here's the php code I'm using:

Code:
$dir = "/home/nolatron/public_html/domain.com/forums";
require($dir . '/src/XF.php');
XF::start($dir);

$forumId = '20';  //Forum ID of where to post thread
$userId = '2';  //User ID # of account to post thread as
$title = "CONTENTS OF SUBJECT";
$message = 'CONTENTS OF POST';

$forum = \XF::em()->find('XF:Forum', $forumId);
$user = \XF::em()->find('XF:User', $userId);
\XF::asVisitor($user, function() use ($forum, $title, $message)
{
    $creator = \XF::service('XF:Thread\Creator', $forum);
    $creator->setContent($title, $message);
    $creator->setPrefix('1');  //Prefix ID # to set new thread as.
    $creator->setIsAutomated();
    $creator->save();
});
 

fly

Well-known member
Sorry to dig up an old thread, but I need some similar help. It appears that @nolatron's solution creates a thread. Any idea how I create a post in an already existing thread?
 

fly

Well-known member
Should someone else wander through here looking for the answer, this is what I came up with. I'm a terrible coder, so while I have no real understanding of this stuff - the code does work!

Code:
<?php
$dir = "/var/www/html";
require($dir . '/src/XF.php');
XF::start($dir);
$app = XF::setupApp('XF\Pub\App');

$userId = 750;
$threadID = 15387;
$message = 'OMG IT WORKS';

$user = \XF::em()->find('XF:User', $userId);
$thread = \XF::em()->find('XF:Thread', $threadID);

\XF::asVisitor($user, function () use ($thread, $message) {
    $replier = \XF::service('XF:Thread\Replier', $thread);
    $replier->setMessage($message);
    $replier->setIsAutomated();
    $replier->save();
});
 
Top