XF 2.1 API Post to Users results in success=true but nothing updates.

PlayerOne

Active member
Posting to /users/{id}/ reports success=true, but no fields seem to change.

I'm a vBulletin 3.x op that just licensed Xenforo and am looking forward to migrating. However, we need to create some custom scripts as we handle our own user creation, and more... So this morning I'm trying to get the APIs to work for me. It seems I can read data fine (users, threads, etc.). However, when I try to Post, it reports back success=true, but no fields change.

Please help. I'm sure I'm missing something really simple...


Key: Super user, All Scopes, Active.

GET users/4/:
{
"user": {
...
"custom_title": "",
...
"timezone": "America/Los_Angeles",
...
"user_id": 4,
...
"website": ""
}
}1

POST users/4/ (trying to update custom_title, timezone, and website fields):
{
"success": true,
"user": {
...
"custom_title": "",
...
"timezone": "America/Los_Angeles",
...
"user_id": 4,
...
"website": ""
}
}

CODE (tried with and without api_bypass_permissions even though it shouldn't apparently be needed). :

<?php

$edituser = 1;
$showuser = 0;

$APIKEY='_<mykey>';
$urlbase = 'https://<mydomainname>/api/';

$headers = array(
'Content-type: application/json; charset=utf-8',
'XF-Api-User: 1',
"XF-Api-Key: $APIKEY"
);

if ($edituser == 1) {

# Thanks https://xenforo.com/community/threads/api-post-thread-with-curl.163601/
$post = [
'timezone' => 'America/New_York',
'custom_title' => 'BigHope',
'website' =>'BigTrouble'
];

$post = http_build_query($post);

$url = $urlbase . 'users/4/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);

echo $json;
}


elseif ($showuser == 1) {
$url = $urlbase . 'users/4/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
$json = curl_exec($ch);
echo $json;
}

?>
 
I got the code working. The problem with the script above is the Content-Type in the $headers array:

  • It can be: 'Content-Type: application/x-www-form-urlencoded; charset=utf-8'
  • It can be: 'Content-Type: application/x-www-form-urlencoded'
  • It can even be left out entirely (at least for my configuration).
  • However, it definitely can't be set to ‘Content-type: application/json’

Also, not the cause of the problem, but there was also a typo in one of the fields being updated for Users in the code above
Instead of ‘website’, it needed to reference ‘profile[website]’. Once I got it working, I quickly noticed that misspelled fields are simply ignored. Even if no fields get updated, if the rest of the call is set up right, it still returns success=true (with no changes to the data).

Other notes:

Setting CURLOPT_RETURNTRANSFER seemed to add no value to me so I'm leaving it out for now.

And instead of:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

It was simpler to simply write:
$ch = curl_init($url);
 
However, it definitely can't be set to ‘Content-type: application/json’
Welcome to my suggestion thread.

I wrote for my own usage add-on with JSON support, but i'm not sure in need to publish him.
 
Back
Top Bottom