Community Documentation

API key authentication using REST Server module

Last updated September 22, 2010. Created by develCuy on March 12, 2009.
Edited by erikwebb, heyrocker, nonsie. Log in to edit this page.

This example should also work fine for 6.1

Follow the instructions as for XMLRPC Server except install and enable REST Server. It does not provide any permissions on its own.

REST Server expects all parameters to be passed in the URL hence the code for using REST based services is a bit different from XMLRPC Server. You need to know the named of the service parameters (uid in user.get)

<?php
$domain
= 'example.com';
$timestamp = (string) time();
$nonce = user_password();
$hash = hash_hmac('sha256', $timestamp .';'.$domain .';'. $nonce .';'.'user.get', 'remote_api_key', 0);
//Note that we are using domain_time_stamp not timestamp and domain_name not domain here
$request_url = $domain.'/method=user.get&uid=0&hash='.$hash.'&domain_name='.$domain.'&domain_time_stamp='.$timestamp.'&nonce='.$nonce;
$session = curl_init($request_url);
curl_setopt($session, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($session, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($session, CURLOPT_HEADER, FALSE);
curl_setopt($session, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($session);
$error = curl_error($session);
curl_close($session);
if (!empty(
$error)) {
  print
'<pre>'.$error.'</pre>';
}
else {
  print
'<pre>'.$result.'</pre>';
}
?>

The code above will:
1. return error code if there was something wrong with the curl request (for example if the url does not exist)
2. return xml if the curl request was successful. This does necessarily mean that the curl request returned a valid result set.
<status>error</status> means there was an error with the service request.
<status>success</status> means the service request was successful.

About this page

Drupal version
Drupal 5.x, Drupal 6.x
Audience
Programmers

Develop for Drupal

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.