0

i was trying to create a new user Programmatically in drupal 6.22 but i can't seem to get it working. I need to make a web service that can do a few things, one being create a new user. This webservice is going to be consumed by a mobile device. I was just trying to test the user_save from outside drupal in just a simple php file and it doesn't seem to work. I just need to know what i'm doing wrong.

Here is my code

<?php
    require_once(includes/bootstrap.inc);
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    // Get an array of roles
    $roles = user_roles();
//Basic account information
$user = array(
    'name' => 'username',
    'pass' => 'Password',
    'mail' => 'email',
    'status' => 1,
   'init' => 'email',
    'roles' => array(array_search('authenticated user', $roles) => 1),
);
// See if the user exists by calling Drupal's user_load()
$existing_user = user_load(array('name' => $user['name']));
    if (!$existing_user->uid) {
    // Save the user
    $user = user_save(NULL, $user);
    } 
?>

EDIT: Added the drupal_bootstrap, still not working for me.

2
  • Could you clarify what "it doesn't seem to work" means?
    – Clive
    Commented Nov 11, 2013 at 12:02
  • The user doesn't appear in the database. When i search for the user by username or email it's not there.
    – Nebseb
    Commented Nov 11, 2013 at 13:12

2 Answers 2

1

Looks like you've included the bootstrap.inc but you haven't actually bootstrapped drupal.

Example:

require_once(includes/bootstrap.inc);
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
1
  • I added the line to the code, yet still no user is showing up in the DB or when i try to search for the user i created by username / email. do I need to do anything else?
    – Nebseb
    Commented Nov 11, 2013 at 13:12
0

I've tested your code, and it works if it is used inside a regular function that is part of a regular module on a regular Drupal 6 site. This suggests that it is not your code for creating a user is wrong, but that the outside Drupal environment you use for testing does not have enough of the Drupal "scaffolding" up and running, and the code fails because somewhere down the line, it attempts to call functions that does not exist in your test environment.

However, just to show an alternative approach, you can create a user by simulating the submission of the standard user creation form, like this:

function createuser_create() {
  $name = 'johndoe';
  $mail = '[email protected]';
  $password = 'Q1w2E3r4T5y';
  $form_state = array();
  $form_state['values']['name'] = $name;
  $form_state['values']['mail'] = $mail;
  $form_state['values']['status'] = 1; // Active user
  $form_state['values']['pass']['pass1'] = $password;
  $form_state['values']['pass']['pass2'] = $password;
  $form_state['values']['op'] = t('Create new account');
  drupal_execute('user_register', $form_state);
  $errors = form_get_errors();
  if (!empty($errors)) {
    drupal_set_message(t('User account creation failed.'), 'error');
  }
  else {
    drupal_set_message(t('User account created.'));
  }
}
3
  • What do you mean by a fully bootstrapped drupal 6 site? The end goal is that I need to be able to write a webservice that i can call from mobile devices to do certain things such as create a new user, look up a user, etc.
    – Nebseb
    Commented Nov 11, 2013 at 17:45
  • It may have been the wrong choice of words. I meant to say that I tested the code in a function against a regular Drupal 6 site. Commented Nov 11, 2013 at 18:00
  • I did the same thing, I put the function in a test.php and threw it in my base folder for my drupal site.
    – Nebseb
    Commented Nov 11, 2013 at 18:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.