I am trying to get a list of all users (not just authors). I have the below script in Codeigniter 4:
class WPUserModel extends Model
{
protected $wordpressApiUrl = '';
protected $username = 'exampleapp';
protected $applicationPassword = 'xxxx yyyy zzzz aaaa bbbb cccc';
public function getAllUsers(): array
{
$client = \Config\Services::curlrequest();
$authHeader = 'Basic ' . base64_encode("{$this->username}:{$this->applicationPassword}");
echo $authHeader;
$users = [];
$page = 1;
$perPage = 100;
try {
while (true) {
$response = $client->request('GET', $this->wordpressApiUrl, [
'headers' => [
'Authorization' => $authHeader,
],
'query' => [
'per_page' => $perPage,
'page' => $page,
'context' => 'edit',
],
]);
$data = json_decode($response->getBody(), true);
if (empty($data)) {
break;
}
$users = array_merge($users, $data);
$page++;
}
} catch (HTTPException $e) {
log_message('error', 'HTTP error: ' . $e->getMessage());
print_r( $e->getMessage());
return [];
} catch (\Exception $e) {
log_message('error', 'An error occurred: ' . $e->getMessage());
print_r( $e->getMessage());
return [];
}
return $users;
}
}
The above give the follow message:
The requested URL returned error: 401
When I run from the command line, I get the same message.
{"code":"rest_forbidden_context","message":"Sorry, you are not allowed to list users.","data":{"status":401}}
I am using a user that has admin rights and an application username and password.
BTW, wordpress give the app password with spaces in it. I have tried with and without the spaces but no luck.
Additionally, I am able to reach the posts endpoint and obtain post data. It is just my application username/password which I wish to obtain a complete list of users which is not working. Also, for info, this is what I issue on the command line which the same error as mentioned above:
curl -u "exampleapp:xxxx yyyy zzzz aaaa bbbb cccc" ";
I do see from the documentation that passwords can be with or without spaces and if there are spaces, wordpress will remove them. Nevertheless, I tried both ways and same result.
How can I get a list of all users? Any tips appreciated.