I'm trying to integrate with a php-based sign-up service we already have in place.

I need to programmatically create accounts for our existing users - and I need to fire some code with each new Drupal signup to create a matching code in the legacy system.

  1. Is there a standard API in 5.x for adding users?
  2. How can I fire some code with each Drupal signup?

Thanks,

Chad

note: I've just installed the actions and workflow modules guessing that they might help but haven't quite figured them out yet. ;)

Comments

pembeci’s picture

To fire some code with each Drupal signup, you can create a simple module where you invoke hook_user and do whatever you want to do in that function.

For adding users automatically, either study the user table and write your own php code to insert the values to the mysql or if you want to do it the Drupal way, create a user object and then use user_save in your custom module.

Hope these will help.

chadj’s picture

That's exactly the "push in the right direction" I needed. Thanks!

- Chadj

pembeci’s picture

First, it may be harder to understand how Drupal works. But once you understand the mechanism it is just beautiful and it seems like there is no limit to what you can do.

mountaineer’s picture

I'm looking into the same thing, only using version 6, but that doesn't really matter. Where would the code for doing this go? I've been looking at the Services module, but for exposing a down and dirty (yet using the Drupal way) interface for creating users, I'm not sure where best to put the code. Create a page with the code for calling user_save in it?

pembeci’s picture

When you choose the input filter as PHP. The more Drupal way will be: create a custom module, register a path at the hook_menu in this module with a callback to your function which will do the user creation. Then all you need to do is visiting the path (URL) you just registered. If you find the latter confusing I suggest reading the Module developer handbook.

just-work’s picture

I get a user Duplicate entry error calling user_save from a page
user warning: Duplicate entry '' for key 2 query: INSERT INTO users (uid, created, access) VALUES (myname, 1223292625, 1223292625)

What does this error mean and why would I be getting it?

Thanks...

pfrilling’s picture

I was receiving the same error you were. After searching through the user module I found that I needed to call the user_save function like this:


$newuser = array(
  'name' => 'name_here',
  'mail' => 'email_here',
  'status' => 1,
  'pass' => 'password_here',
);

$user = user_save('', $newuser);

After this code is called, the new user is created and automatically logged in.

TPerkins’s picture

If you don't have time to learn Drupal's module system, or need to do something with the API really quickly and don't want to bother making a module, you can can create a php file in your root Drupal directory.

Just include the bootstrap at the very beginning like so and your ready to go.

<?php
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

//code using the Drupal API

?>