I'm trying to delete user account via REST API.
Here is my code:
// .xml asks for xml data in response.
$service_url = 'http://www.example.com/rest/user/login';
$post_data = [
'username' => 'test',
'password' => 'pass',
];
// Format post data as application/x-www-form-urlencoded.
$post_data = http_build_query($post_data, '', '&');
// Set up the request.
$curl = curl_init($service_url);
// Have curl_exec return a string.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// Do a POST.
curl_setopt($curl, CURLOPT_POST, TRUE);
// POST this data.
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
// Output to command line
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
// Make the request.
$response = curl_exec($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
$xml = new SimpleXMLElement($response);
$session_cookie = $xml->session_name . '=' . $xml->sessid;
// Get token.
$curl_get = curl_init();
curl_setopt_array($curl_get, [
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://www.example.com/services/session/token',
]);
$csrf_token = curl_exec($curl_get);
curl_close($curl_get);
// A user ID.
$id = 1500;
// The token.
$token = $csrf_token;
// Set up the curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/rest/user/$id?access_token=$token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
$response = curl_exec($ch);
// Close curl resource to free up system resources.
curl_close($ch);
It returns:
Access denied for user anonymous
How do I need to write the code to delete a user?