Building a custom API using a RESTful module in Drupal 7

I am trying to figure out how to create a RESTful API for CRUD for users of a Drupal 7 instance. It would be preferable to use the following module because other parts of it use it: https://github.com/RESTful-Drupal/restful If there is a quieter module there , I would like to give him a chance.

The documentation shows a bit how to use it with articles. What do I need to change for users? Is there a tutorial I can follow?

+3


source to share


1 answer


You will have more joy and it will be more secure if you create your endpoint in a module, just for user registration.

First, in your .module use the hook_menu function

/*
 * hook_menu
 */
function yourmodule_menu() {
    $items['yourmodule/user/register'] = array(
        'page callback' => 'yourmodule_user_register',
        'access arguments' => array('access content'),
    );
    return $items;
}

      



Then create a callback function:

/*
 * hook_menu page callback
 */
function yourmodule_user_register() {

    // if you want to send the payload using content type 'application/json'
    $json = file_get_contents('php://input');
    $obj = json_decode($json);
    // otherwise use $_POST

    // if you have autoloader set up
    $register = new YourmoduleUserRegister($obj);
    // otherwise add in your logic here to validate and save user data

}

      

After you have verified your user credentials using the drupal API methods user_load_by_mail () and user_load_by_name (), use user_save () to register the user. You can rename the original urls and do other security stuff here.

0


source







All Articles