Resource icon

How to call the XenForo API

Sim

Well-known member
Sim submitted a new resource:

How to call the XenForo API - A tutorial with working examples of how to call the XenForo API

The new REST API in XenForo 2.1 lets us integrate third party apps with XenForo in new ways.

The key thing to remember about this API implementation is that it follows REST principals (REpresentational State Transfer) which simply means that there is a uniform set of operations that you can perform on the web service that exposes the API.

Most developers will be familiar with the two common HTTP methods we use in most web applications: GET to retrieve information from a web server (eg view...

Read more about this resource...
 
Thanks for the start.

Would you know how to batch update the secondary_user_groups for multiple users at the same time?

This is what I have so far, except it only updates one user_group for each user, and not the whole array.

PHP:
<?php

include('httpful.phar');

use \Httpful\Mime;

$userForums = [
    3 => [29,17,19,15,18],
    2156 => [17,22,37],
    32577 => [22,16,23,28,37]];

foreach($userForums as $user => $forums) {
    foreach ($forums as $forum) {
        $response = updateUserForum($user, $forum);

        echo $response->body->success ."\n";
    }
}

function updateUserForum($user, $forum) {
    $url = "https://test-forum.myurl.com/api/users/$user/";
    $apiUser = admin;
    $apiKey = 'abcdefghijklmopqrstuvwxyz';
    return \Httpful\Request::post($url)
        ->addHeaders(['XF-Api-User' => $apiUser, 'XF-Api-Key' => $apiKey])
        ->body("secondary_group_ids[]=$forum")
        ->sendsType(Mime::FORM)
        ->send();
}

Thanks.
 
Firstly, I want to say that this is some hugely helpful info here, thanks (y)

I have been putting the API through its paces lately, using curl (and the examples here) and am either doing something silly or have tripped over an XF bug. Before reporting the bug I thought I'd check here, since more likely than not I'm missing something.

When I attempt to delete things (e.g. a post), and provide a line in the curl invocation containing --data-urlencode 'hard_delete=false' \, the API attempts to perform a hard delete. This is not at all what I expected given the "false" in my parameter. When I omit that line entirely, I get the expected soft delete. What am I missing here?
(For info: I originally included that line because I intend to do a bunch of hard deletions in due course, and wanted to make the test versions of the script as close as possible to the final product...)
[The server on which I'm testing this is on 2.2.8p1 but I've reviewed all of the change logs since then and none of them mention anything relevant to this behaviour.]

[Edit: argh, I just realised that I hadn't tried hard_delete=0 - when I tested it just now, it performed a soft delete... aarrrggghhh :rolleyes: ]
 
Top Bottom