Resource icon

Using XenForo Code in other applications

Lukas W.

Well-known member
Lukas W. submitted a new resource:

Using XenForo Code in other Applications - Some often-asked basics

Introduction
One of the most-asked questions in the XenForo Dev Section is: How do I access data from XenForo in other applications? Generally speaking, there is two ways you can go about this: If your application is in a different language than PHP, or resides on a different server, you can utilize the built-in API to access most data, and push and pull content from and to your XenForo installation. If you're on the same server, and your other...

Read more about this resource...
 
@Lukas W. thank you for you instruction!

I have some problems with getting current logged in user.

I logged in on my forum.
I created external script test.php in forum root with following content:
PHP:
<?php

require(__DIR__ . '/src/XF.php');
XF::start(__DIR__);
var_dump(XF::visitor());
But when I open this script in browser (test script have access to all XF cookies) and look at var_dump output, I can't see any current logged in user data. All fields are empty, like from guest visitor.

Please, help me to resolve this issue.
 
I'm trying to load the XF UI (header and footer) on an external PHP application that I want to integrate with XenForo.

I followed the template section and $templater->renderTemplate('public:PAGE_CONTAINER', $pageParams) is returning string(0) ""


code:
PHP:
// Initialize XF
define('CWD', (($getcwd = getcwd()) ? $getcwd : '.'));
$dir = CWD . '/forum';

require($dir . '/src/XF.php');

\XF::start($dir);

$app = \XF::setupApp('XF\Pub\App');
$app->start();


// Get visitor
$visitor = \XF::visitor();


// Initialize template engine
$templater = \XF::app()->templater();
$globalTemplateData = \XF::app()->getGlobalTemplateData();
$templater->addDefaultParam('xf', $globalTemplateData);
$templater->setStyle(\XF::app()->style(\XF::visitor()->style_id));
$templater->setLanguage(\XF::app()->userLanguage(\XF::visitor()));
$templater = \XF::app()->templater();


// Create the wrapper
$pageParams = $myPageParams;
$pageParams['content'] = '<b>Hello World</b>';

$wrappedResponse = $templater->renderTemplate('public:PAGE_CONTAINER', $pageParams);

// Returns string(0) ""
var_dump($wrappedResponse);


Any ideas? I guess I am missing a step or the params need to be set in some way?

Visitor data is visible if I var dump it, so XF is initialized.
 
Back
Top Bottom