How to delete a user via REST 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' =&gt; 'test',
  'password' =&gt; 'pass',
];

// Format post data as application/x-www-form-urlencoded.
$post_data = http_build_query($post_data, '', '&amp;');

// 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-&gt;session_name . '=' . $xml-&gt;sessid;

// Get token.
$curl_get = curl_init();
curl_setopt_array($curl_get, [
  CURLOPT_RETURNTRANSFER =&gt; 1,
  CURLOPT_URL            =&gt; '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?
First you need to login:

$loginData = ['username' =&gt; 'yourusername', 'password' =&gt; 'yourpassword'];

$ch = curl_init("http://pathtodrupal.com/endpoint/user/login.json");

curl_setopt_array($ch, [
  CURL_OPT              =&gt; TRUE,
  CURLOPTRETURNTRANSFER =&gt; TRUE,
  CURLOPT_HTTPHEADER    =&gt; ['Content-Type: application/json'],
  CURLOPT_POSTFIELDS    =&gt; json_encode($loginData),
]);

$response = curl_exec($ch);

$responseData = json_decode($response, TRUE);


Once you send a POST request to login you get the following response back:


  {u'session_name': u'SESScxx45656f30', u'sessid':
  u'mOE9NdG6Bk3EADSvKJHGGy8jgkQ', u'token':
  u'-HVkXhARIynpmJxow5NUIYTRXSc', u'user': {u'status': u'1', u'picture':
  u'0', u'uuid': u'514c987e7-15f0-43cc-9c6f-53279c216f30', u'uid': u'1',
  u'language': u'', u'created': u'1310006971', u'init':
  u'morinfo@dbasepu.com', u'timezone': u'America/New_York', u'access':
  u'1429109927', u'theme': u'', u'roles': {u'2': u'authenticated user'},
  u'signature': u'', u'signature_format': u'1', u'mail':
  u'mor@morinfo.com', u'login': 1429111638, u'data': {u'ckeditor_width':
  u'100%', u'form_build_id': u'form-60703ffc3';lk6c0fe03cebe9',
  u'ckeditor_lang': u'en', u'ckeditor_show_toggle': u't',
  u'last_category_id': 1, u'ckeditor_auto_lang': u't',
  u'ckeditor_default': u't', u'site_access': {u'1': u'1'},
  u'last_site_id': 1}, u'name': u'bewmrn'}}


In your subsequent requests you want set the the set in the header the X-CSRF-Token to $response['token'] and Cookie to $response['session_name'].=.$response['sessid']. This should work.