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.
 
Top