XenAPI - XenForo PHP REST API

XenAPI - XenForo PHP REST API 1.4.2

No permission to download

Contex

Well-known member
Seems to work pretty well actually :)

I used the following

Code:
api.php?action=getThreads&hash=My_API_Key&node_id=4&order_by=post_date&order=desc&limit=5&grab_content&content_limit=1

To grab the 5 latest threads in the announcement forums and their content.
Glad to hear that :)
 

faeronsayn

Well-known member
I've learned that the output is quite crazy, in terms of the arrays at least x.x

This is the PHP array that the jSON object is converted to, I used print_r to output the whole thing to show you..

Code:
Array ( [count] => 1 [threads] => Array ( [14] => Array ( [thread_id] => 14 [node_id] => 4 [title] => Testing another [reply_count] => 0 [view_count] => 0 [user_id] => 59 [username] => Faeron [post_date] => 1369261979 [sticky] => 0 [discussion_state] => visible [discussion_open] => 1 [discussion_type] => [first_post_id] => 25 [first_post_likes] => 0 [last_post_date] => 1369261979 [last_post_id] => 25 [last_post_user_id] => 59 [last_post_username] => Faeron [prefix_id] => 0 [content] => Array ( [count] => 1 [content] => Array ( [25] => Array ( [post_id] => 25 [thread_id] => 14 [user_id] => 59 [username] => Faeron [post_date] => 1369261979 [message] => this is another thread [ip_id] => 272 [message_state] => visible [attach_count] => 0 [position] => 0 [likes] => 0 [like_users] => a:0:{} [warning_id] => 0 [warning_message] => ) ) ) ) ) )

So what do I feel the problem is?

Well first off, you have an array under 'threads' and that array has another called '14'. The 14 represents the thread ID, however, how am I suppose to grab variables when I don't know the thread ID prior to outputting it? What I am trying to say is that the array's name is a variable that changes, and I cannot get that changed variable without knowing the thread id.

For example

To get the title of the thread I use this

Code:
$phpNews_array['threads']['14']['title']

But let's say that thread is no longer the latest thread, the code above becomes obsolete. Because the new thread's title, is not accessible by that code, because the array's name is now the new thread_id and not '14'.

I can't make my php code work for all threads because I don't have access to the thread_id unless I know the thread_id, which doesn't work x.x

If there is any other way to go about this, please tell me. I still find this a little annoying.

Furthermore, to get the message of the thread... I have to do the following..

Code:
$phpNews_array['threads']['14']['content']['content']['25']['message']

Again the number '25' is the post ID, which I don't have access to until I am into that array.

I am assuming you would loop through arrays or something of that sort?

EDIT: Using the following works

Code:
reset($phpNews_array);
$first = array_keys($phpNews_array['threads']);
print_r($first);
$first = $first[1];
$second = array_keys($phpNews_array['threads'][$first]['content']['content']);
$second = $second[0];
echo $phpNews_array['threads'][$first]['content']['content'][$second]['message'];
 

Contex

Well-known member
I've learned that the output is quite crazy, in terms of the arrays at least x.x

This is the PHP array that the jSON object is converted to, I used print_r to output the whole thing to show you..

Code:
Array ( [count] => 1 [threads] => Array ( [14] => Array ( [thread_id] => 14 [node_id] => 4 [title] => Testing another [reply_count] => 0 [view_count] => 0 [user_id] => 59 [username] => Faeron [post_date] => 1369261979 [sticky] => 0 [discussion_state] => visible [discussion_open] => 1 [discussion_type] => [first_post_id] => 25 [first_post_likes] => 0 [last_post_date] => 1369261979 [last_post_id] => 25 [last_post_user_id] => 59 [last_post_username] => Faeron [prefix_id] => 0 [content] => Array ( [count] => 1 [content] => Array ( [25] => Array ( [post_id] => 25 [thread_id] => 14 [user_id] => 59 [username] => Faeron [post_date] => 1369261979 [message] => this is another thread [ip_id] => 272 [message_state] => visible [attach_count] => 0 [position] => 0 [likes] => 0 [like_users] => a:0:{} [warning_id] => 0 [warning_message] => ) ) ) ) ) )

So what do I feel the problem is?

Well first off, you have an array under 'threads' and that array has another called '14'. The 14 represents the thread ID, however, how am I suppose to grab variables when I don't know the thread ID prior to outputting it? What I am trying to say is that the array's name is a variable that changes, and I cannot get that changed variable without knowing the thread id.

For example

To get the title of the thread I use this

Code:
$phpNews_array['threads']['14']['title']

But let's say that thread is no longer the latest thread, the code above becomes obsolete. Because the new thread's title, is not accessible by that code, because the array's name is now the new thread_id and not '14'.

I can't make my php code work for all threads because I don't have access to the thread_id unless I know the thread_id, which doesn't work x.x

If there is any other way to go about this, please tell me. I still find this a little annoying.

Furthermore, to get the message of the thread... I have to do the following..

Code:
$phpNews_array['threads']['14']['content']['content']['25']['message']

Again the number '25' is the post ID, which I don't have access to until I am into that array.

I am assuming you would loop through arrays or something of that sort?

EDIT: Using the following works

Code:
reset($phpNews_array);
$first = array_keys($phpNews_array['threads']);
print_r($first);
$first = $first[1];
$second = array_keys($phpNews_array['threads'][$first]['content']['content']);
$second = $second[0];
echo $phpNews_array['threads'][$first]['content']['content'][$second]['message'];
I'm sorry about that, I fixed it in the recent commit.
https://github.com/Contex/XenAPI/commit/c8b9f997ec2233f2e245d43c0b7357c2afa39504
https://raw.github.com/Contex/XenAPI/dev/net/xenapi/XenAPI/api.php

In addition, I made the content_limit 1 by default, so you'll only have to use the grab_content parameter to grab the thread's first post.

Let me know if there's anything else.
 

faeronsayn

Well-known member
@Contex I am wondering if you could set the discussion_state as a parameter when returning threads. It's quite important and might be even better to set discussion_state as visible by default so it only returns threads that are actually visible to the user. Currently I am not able to use it as a parameter and therefore deleted threads are being returned as well.

Additionally, how can I make sure that the current user / guest can only see what they have the permission to see? Basically, if there is a thread being returned from the admin area, I don't want normal members to see it. How does the plugin handle that, especially when using a hash to grab the content.
 

Contex

Well-known member
@Contex I am wondering if you could set the discussion_state as a parameter when returning threads. It's quite important and might be even better to set discussion_state as visible by default so it only returns threads that are actually visible to the user. Currently I am not able to use it as a parameter and therefore deleted threads are being returned as well.

Additionally, how can I make sure that the current user / guest can only see what they have the permission to see? Basically, if there is a thread being returned from the admin area, I don't want normal members to see it. How does the plugin handle that, especially when using a hash to grab the content.
Currently, if you use the API key instead of a user hash; all the threads will be returned, no matter if they are marked as visible, moderated or deleted.

However, if you use the API key with the grab_as parameter or use an user hash, it will use users permissions instead.
The user will get a list of threads depending on their permissions, the API goes through each thread that was grabbed for the query and checks if the user "canViewThread", if not; the thread will be removed from the list.

I use XenForo's models for the permissions, these models have functions that checks if the user is permitted to do such things.
For the getThread action, I use the model called "Thread" and I use the function called "canViewThread" to check if the user is permitted to view the thread.
Do note that permissions gets ignored if you're only using the API key.

I'll see if I can get a discussion_state parameter added, I'm currently on a holiday but I should be able to add it sometime next week.

Let me know if there's anything else.
 

faeronsayn

Well-known member
Currently, if you use the API key instead of a user hash; all the threads will be returned, no matter if they are marked as visible, moderated or deleted.

However, if you use the API key with the grab_as parameter or use an user hash, it will use users permissions instead.
The user will get a list of threads depending on their permissions, the API goes through each thread that was grabbed for the query and checks if the user "canViewThread", if not; the thread will be removed from the list.

I use XenForo's models for the permissions, these models have functions that checks if the user is permitted to do such things.
For the getThread action, I use the model called "Thread" and I use the function called "canViewThread" to check if the user is permitted to view the thread.
Do note that permissions gets ignored if you're only using the API key.

I'll see if I can get a discussion_state parameter added, I'm currently on a holiday but I should be able to add it sometime next week.

Let me know if there's anything else.

Would it be smart to use grab_as on the currently logged in user? Meaning the thread list it pulls could be different for each and every user. Would this be an issue in terms of speed since each user will have their own set of threads? I don't see any example of grabbing as a guest, is this possible?

As for discussion_state parameter I am in no hurry. I've decided to just pull 15 threads and then in a loop just run an if statement and display only 5 visible threads.
 

Contex

Well-known member
Would it be smart to use grab_as on the currently logged in user? Meaning the thread list it pulls could be different for each and every user. Would this be an issue in terms of speed since each user will have their own set of threads? I don't see any example of grabbing as a guest, is this possible?

As for discussion_state parameter I am in no hurry. I've decided to just pull 15 threads and then in a loop just run an if statement and display only 5 visible threads.
The grab_as parameter was designed to be used for the currently logged in user, but if don't want to use the API key you can use the user hash instead.
There shouldn't be any issues when it comes to speed, unless you grab the content of the thread and grab all the threads (limit it to 20 threads per page for example).

Regarding guests; good thing you brought that up; I'll have a look for a solution sometime next week and add the discussion_state parameter while I'm at it.
 

faeronsayn

Well-known member
The grab_as parameter was designed to be used for the currently logged in user, but if don't want to use the API key you can use the user hash instead.
There shouldn't be any issues when it comes to speed, unless you grab the content of the thread and grab all the threads (limit it to 20 threads per page for example).

Regarding guests; good thing you brought that up; I'll have a look for a solution sometime next week and add the discussion_state parameter while I'm at it.

Great to hear. Thanks again, I am heavily using this plugin with WordPress / xenForo.
 

Marcuss

Member
Hello,

Very good work and thank you very much for this API

I have a small problem with the "authenticate" function that does not return me the error value but a NULL value when I did "var_dump ($ json);"

Here is my code:

PHP:
$contents = file_get_contents("http://www.--.fr/api.php?action=authenticate&username=Marcuss&password=33");
$json = json_decode($contents, true);
var_dump($json);

If I put the URL in the browser, I have to have this result:

{"error":5,"message":"Authentication error: \"Invalid username or password!\""}

But, with "var_dump", I have just : "NULL"

Thank you in advance for the quick help :)

PS: Sorry for the mistakes, I'm French

Marcuss.
 

faeronsayn

Well-known member
@Contex I am wondering if it would be possible to implement an alert feature. Something that can be used to generate xenForo alerts for users from other platforms?
 
Last edited:

Contex

Well-known member
@Contex I am wondering if it would be possible to implement an alert feature. Something that can be used to generate xenForo alerts for users from other platforms?
Yes.

I'm going to do a code session this weekend as I've been filled up with projects from work the last couple of weeks and haven't had the time to spend time with any of my personal projects.
 

Contex

Well-known member
Hello,

Very good work and thank you very much for this API

I have a small problem with the "authenticate" function that does not return me the error value but a NULL value when I did "var_dump ($ json);"

Here is my code:

PHP:
$contents = file_get_contents("http://www.--.fr/api.php?action=authenticate&username=Marcuss&password=33");
$json = json_decode($contents, true);
var_dump($json);

If I put the URL in the browser, I have to have this result:



But, with "var_dump", I have just : "NULL"

Thank you in advance for the quick help :)

PS: Sorry for the mistakes, I'm French

Marcuss.
Sorry for the late reply, it works just fine here.

I'd try to debug it a bit, check the error logs and so on.
 

vbezruchkin

Member
Hi,

Thanks for this great plugin. Is there any plan to add one more method that would delete a specific user?
No rush at all. Just curios if you had any requests for that and want to implement it.

Thanks!
 

DeltaHF

Well-known member
This looks like a great plugin. (y)

Just a question: does this add any columns to any of the tables, or is it purely a wrapper which exposes the data? I know the other RESTful API plugin adds some columns. I have a very large forum so this is something that I'm curious about. (Edit: Silly question! It doesn't.)
 
Last edited:

Bill.D

Active member
Hey All,

Does that API have any PUT options. For instance to have a site to be able to automatically post to a Thread OR create a Thread. If not does anyone know a product that can do that. Just some back ground; We have a Main WP site and a Separately hosted XF site. We want posts to the WP site to form Threads on the XF site which is at a subdomain ( i.e. forum.domain.com ). We already know about 2 WP plugins that do this, but its only for sub-drectory forums ( i.e. domain.com/forum ) where xenforo is on the same server. I feel as though this scenario really needs a plugin on both sides; which is why I started looking up API as it seemed to be part of the right solution.

Thanks,
-Bill
 

VintagePC

Member
Anyone else having an issue with the getUser() function not returning an email address for all users even if they have one entered? I encountered this problem while implementing external authentication with the API. Some users were failing to log in because no email was supplied in the JSON response. (but it looks like all other parameters were).
 

Contex

Well-known member
Sorry for the late replies, I've been AWOL for a while.

Hi,

Thanks for this great plugin. Is there any plan to add one more method that would delete a specific user?
No rush at all. Just curios if you had any requests for that and want to implement it.

Thanks!
Yes, it's on the todo list: https://github.com/Contex/XenAPI/issues/6

Hey All,

Does that API have any PUT options. For instance to have a site to be able to automatically post to a Thread OR create a Thread. If not does anyone know a product that can do that. Just some back ground; We have a Main WP site and a Separately hosted XF site. We want posts to the WP site to form Threads on the XF site which is at a subdomain ( i.e. forum.domain.com ). We already know about 2 WP plugins that do this, but its only for sub-drectory forums ( i.e. domain.com/forum ) where xenforo is on the same server. I feel as though this scenario really needs a plugin on both sides; which is why I started looking up API as it seemed to be part of the right solution.

Thanks,
-Bill

Sadly, it's not a true RESTful API (all request methods are handled the same, I haven't tried doing a PUT request yet but I recon it should work).
I'm currently working on a rewrite which would make it a true RESTful API and easy for me/others to add new functions.
The master branch does not currently support creating threads, but the dev branch has that implemented, however; the master branch does have creation for threads.

Anyone else having an issue with the getUser() function not returning an email address for all users even if they have one entered? I encountered this problem while implementing external authentication with the API. Some users were failing to log in because no email was supplied in the JSON response. (but it looks like all other parameters were).
I haven't heard about that before, would you mind creating an issue on Github so I could take a further look?
 

VintagePC

Member
I haven't heard about that before, would you mind creating an issue on Github so I could take a further look?

I'll see if I can gather some more info on the issue I'm seeing later today, and will provide it to you. :)

Near as I can tell at current, it seems to be only when issuing 'getUser' with an authenticated hash. The email is returned correctly when querying using the API key.
 
Last edited:
Top