XF 2.2 Why won't this add a user via the API?

PPC Coach

Member
Here's my code:
<?php
define('XENFORO_API_KEY', 'my_API_key_here');
define('XENFORO_BASE_URL', 'https://members.printondemandcoach.com/api');

$url = XENFORO_BASE_URL . '/users';
$data = [
'username' => 'testuser',
'email' => 'testuser@example.com',
'user_group_id' => 2
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'XF-Api-Key: ' . XENFORO_API_KEY,
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);

echo $response;

And I get this response:

Not Found
The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
 
Did you echo out the url and try it manually? it should work with a common error like missing api key, etc

eg, a get of user 1:
yoursite.com/api/users/1

it seems to work on your site, so the path likely is missing a slash or something.
 
Back
Top Bottom