XF 2.1 Getting user instance from XF?

rhs

Active member
Hello,

in XF 1.5x I was able to use ...
PHP:
$xf_user = XenForo_Visitor::getInstance();
... to access the data of the user who is currently logged into the XF. Using this data, I then created an automatic login in an external wiki (Dokuwiki).

How do I get this data with XF 2.1? I assume with the REST API -> GET me/. Is there a simple example that shows the name of the registered user?

Thank you.
 
That's internal from an add-on?. What I need would be external via PHP:

Code:
<?php

$headers = array(
    'Content-type: application/json',
    'XF-Api-Key: wg8N4eyz2DhBXdfCZU7AZ23mCnmR1UDc',
);

$url = 'https://www.myforum.de/forum/api/me/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
$json = curl_exec($ch);

echo $json;

The call only gives me that back:

Code:
{ "me": { "user_id": 0, "username": null } }

Apparently "me" is not the right point in the REST API.
 
PHP:
\XF::visitor()
would be the XF 2 equivalent

PHP:
<?php

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

XF::start($dir);

$xf_user = \XF::visitor();

print_r($xf_user);

Only gives me an empty user object, but I need the actual user ...
 
PHP:
\XF::visitor();

Will deliver you the currently logged in user. If you receive a default user with no values (aka. empty), that's a guest user, and means you're not logged in. That can for example happen if your script doesn't have access to cookies/session of your main site when you visit it, so your script can't actually log you in. It's the same principle for the REST API for what it's worth.
 
So the above code is correct?

I am probably logged in, but I only get empty user data. The test script is in the root of the forum.
 
Last edited:
I am definitely logged in. I have called the test script also over a new menu point in xf, also there comes only empty data.

I don't know what else to check. With XF 1.5 it worked immediately with the old method. Does the script work for you without any changes if it is in the forum root?
 
These two lines were still missing ...

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

So it works now. What I find a little strange is that here it is assumed that everyone feels at home in XF code. From you professionals could also come a tip/example, how it works correctly ...
 
What I find a little strange is that you copy some code together, come here for a specific question, and then get angry cause people don't fix the entirety of your code for you? If you don't know how to start the app correctly, then ask so, and don't blame us for assuming that you knew what you were doing. :unsure: People answer a lot of questions around here, way more than on lot of other communities already, all you gotta do is ask.
 
I'm not angry ...
I don't even know how I get this data in XF 2.1. That's why I asked. Then come up with the answer, use \XF::visitor(). So far so good, but that's only half the battle ...

But anyway, it works now, and I thank you for your help.
 
That's internal from an add-on?. What I need would be external via PHP:

Code:
<?php

$headers = array(
    'Content-type: application/json',
    'XF-Api-Key: wg8N4eyz2DhBXdfCZU7AZ23mCnmR1UDc',
);

$url = 'https://www.myforum.de/forum/api/me/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
$json = curl_exec($ch);

echo $json;

The call only gives me that back:

Code:
{ "me": { "user_id": 0, "username": null } }

Apparently "me" is not the right point in the REST API.

This didn't work because you hadn't set a user in the API. The API is completely stateless and independent of anything else - you'd need to use a super user key and specify the XF-API-User header with the relevant user_id.
 
Thank you Liam. I had already tried this, but had not received any data. I solved it now with \XF::visitor() and it works like before with XF 1.5. But I only change a little. This was about connecting DokuWiki to the forum in such a way that a registered user can call the Wiki and is immediately authorized. This now also works with XF 2.1. If you want to view/test it: www.cargobikeforum.de (german).
 
Back
Top Bottom