Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / remote

Remote Operations on Drupal Objects. Technical Details.

0.00/5 (No votes)
28 Feb 2013CPOL9 min read 10.5K  
In this article, I'll explain the communication technical details.

Introduction

In the previous article, Drupal with Android integration: make posts and upload photos, I wrote an introduction about how to integrate Drupal with Android. In this article, I'll explain the communication technical details. If you need some guidelines, about how XML-RPC is implemented in Drupal or you need information about the remote methods it exposes, this article will be useful for you. If you need to develop an app that will create, edit, or delete pages, files, comments, etc., this article will give you information about how to make it.

XML-RPC Background

XML-RPC is a lightweight remote procedure calls protocol. It does not require methods description as, for example, with SOAP. XML-RPC server exposes a set of remote methods, with input arguments and return value types. It supports the following data types:

  • int integer value
  • double double-precision floating point value
  • boolean boolean value
  • string string value
  • dateTime date/time value
  • base64 base64 encoded binary data
  • array array of values
  • struct structure, where each member has name and value (similar to associative arrays)

int, double, boolean, string, dateTime and base64 are primitive types. array and struct are complex types, consisting of other values.

Examples

Method takes one input argument as structure, and returns structure value.

Method takes two string input arguments, and returns structure value.

  • struct node.create(struct node)
  • struct user.login(string, string)

Drupal exposes a set of methods through XML-RPC. Drupal xml-rpc servers is implemented in modules, in hook_xmlrpc (or by defining service resources, but this method is not in scope of this article). There are built-in methods, and you can create your own.

Most of the operations on Drupal objects are accessible through XML-RPC, are CRUD (create-read-update-delete). For example, for node operations are: node.create, node.retrieve, node.update, node.delete, node.index. The same is for applied for the most Drupal objects: comments, users, files, taxonomy terms. Only replace node with appropriate object type in method name. Most of the objects are identified by its integer id: nid (node identifier), cid (comment identifier), fid (file identifier), and so on. retrieve, update, and delete methods takes object id as first argument.

Authorization

Authorization must be performed before calling the methods, that require an authorized user. Most of the methods described in this article must be called by an authorized user.

C#
struct user.login(string username, string password)

Method to perform user login. It takes username and password arguments, and returns structure containing session params (sessid and session_name) and nested structure with information about user (email, uid, etc.).

If you need to perform remote actions that require authorization, you must call this method first. You need to store sessid and session_name returned values, as they identify an open user session. These values will be sent as cookie for all subsequent remote calls.

C#
user.logout()

Perform logout. This method must be called when more actions will be performed to close Drupal session.

Users Operations

User information is passed in structure with the following members:

  • name - user name
  • mail - email address
  • pass - plain text unencrypted password

These properties can be passed, but are optional:

  • status - 0 for blocked, otherwise will be active by default
  • notify - 1 to notify user of new account, will not notify by default

Roles can be passed in a roles property, which is an associative array formatted with '<role>' => '<role>', not including the authenticated user role, which is given by default.

This structure is passed to create, register and update methods as input argument. Users are identified by integer value - uid (user identifier).

C#
struct user.create(struct account)

Create the new user, specified by account structure. This method may be called only by authorized users with 'administer users' permission.

C#
struct user.retrieve(int uid)

Retrieves a user account information. User is specified by uid (user identifier). Returned structure contains user account information.

C#
struct user.update(int uid, struct account)

Update the existing user, specified by uid (user identifier). New user's data is specified by accout input argument. Calling user must have 'administer users' permission.

C#
boolean user.delete(int uid)

Removes the user, specified by uid. Returns true, if user is successfully deleted, false - otherwise.

C#
array user.index(int page, string fields, array parameters, int pagesize)

Get list of users. All arguments are optional. This method can split result to pages. page argument specifies the page number to return, fields - what fields to be returned, parameters - is the parameters used to build SQL WHERE clause, pagesize - specifies size of the page. Returned value is an array of structures with requested users. If method is called without arguments, it will return all users. For example: you have 100 users, setting pagesize = 20 and page = 2, will return users 40-60.

C#
struct user.register(struct account)

Register a new user. This method may be called by only unauthorized user (no user.login call before it). User's data is passed in account argument. The difference between register and create methods is that register is called by an anonymous user, while create can be called only by user with 'administer users' permission.

Node Operations

Node operations are CRUD (create-read-update-delete). Node is identified by nid (node identifier), and node information is passed and returned in structure with the following members:

  • type - node type (page, story, etc.)
  • title - node title
  • body - node body
  • nid - node identifier (returned by retrieve, create, update methods)
C#
struct node.create(struct node)

Creates a new node. It takes structure containing information about node to create. Method returns structure with created node information. Input and output structure differs in that, output is assigned nid (node identifier).

C#
struct node.retrieve(string nid)

Retrieves the node content. Input argument is nid (node identifier). Returned value is structure with node information.

C#
struct node.update(string nid, struct node)

Updates an existing node. Input arguments are nid and structure with node information (node type, nid, title, body, etc.). Method returns structure with updated node information.

C#
boolean node.delete(string nid)

Remove the node, specified by nid (node identifier). Returns true, if node successfully deleted, false - overwise.

C#
array node.index(int page, string fields, array parameters, int pagesize)

Get list of nodes. All arguments are optional. This method can split result to pages. page argument specifies the page number to return, fields - what fields to be returned, parameters - is the parameters used to build SQL WHERE clause, pagesize - specifies size of the page. Returned value is an array of structures with requested nodes. If method is called without arguments, it will return all nodes.

For example: you have 100 nodes, setting pagesize = 20 and page = 2, will return nodes 40-60.

C#
array node.files(int nid, int file_contents)

This method returns files associated with a node. Node is specified by nid (node identifier). file_contents argument specifies, whether or not to return file contents. Returned value is an array of structures with file information (see File manipulation section for details, about file structures).

C#
array node.comments(int nid, int count, int offset)

Get node comments. Node specified by nid argument. count specifies comments count to return, offset specifies index of the first comment to return. Method returns an array of structures with comments information (see Comments operations section for details, about comment structure). To call this method, comment module must be enabled.

Comments Operations

Comment operations are use the same CRUD approach, as other objects. Comment information is passed in structure with the following members:

  • author - name of the author of comment
  • date - date of the comment. Value 'now'
  • subject - the subject of the comment
  • comment_body
  • nid - node identifier
  • pid - parent comment id
  • format - comment format
C#
struct comment.create(struct comment)

Create a new comment. Comment data specified by by comment argument. Method returns created comment information.

C#
struct comment.retrieve(int cid)

Returns the comment, specified by cid (comment identifier). Method returns struct with comment information.

C#
struct comment.update(int cid, struct comment)

Update an existing comment. Comment specified by cid argument, new comment data by comment argument. Method returns updated comment information.

C#
boolean comment.delete(int cid)

Delete the comment, specified by cid (comment identifier). Returns true if comment is successfully removed, false - otherwise.

C#
array comment.index(int page, string fields, array parameters, int pagesize)

Get list of comments. All arguments are optional. This method can split result to pages. page argument specifies the page number to return, fields - what fields to be returned, parameters - is the parameters used to build SQL WHERE clause, pagesize - specifies size of the page. Returned value is an array of structures with requested comments. If method is called without arguments, it will return all comments.

For example: you have 100 comments, setting pagesize = 20 and page = 2, will return comments 40-60.

C#
int comment.countAll(int nid)

Method returns the number of comments for the given node. Node is specified by nid (node identifier).

C#
int comment.countNew(int nid, int since)

Method returns the number of new comments on a given node since a given timestamp. nid argument specifies the node, since specifies timestamp since that to return comments.

File Operations

File operations use the same CRUD approach, as other objects. Files identified by integer fids (file identifiers), and file described by the structure with the following members:

  • filename - file name
  • filepath - file path
  • file - file data encoded as base64
C#
struct file.create(struct file)

Create a new file. File input argument is structure, describing file. Method returns structure with created file information. Input and output structure differs in that output has assigned fid (file identifier).

C#
struct file.retrieve(int fid, int file_contents)

Retrieve the file, specified by fid (file identifier). file_contents argument specifies, whether or not to return file content. Structure returned by method, contains file description.

C#
boolean file.delete(int fid)

Remove the file, specified by fid (file identifier). Returns true if file is successfully deleted, false - otherwise.

C#
array file.index(int page, string fields, array parameters, int pagesize)

Get list of files. All arguments are optional. This method can split result to pages. page argument specifies the page number to return, fields - what fields to be returned, parameters - is the parameters used to build SQL WHERE clause, pagesize - specifies size of the page. Returned value is an array of structures with requested files. If method is called without arguments, it will return all files.

For example: you have 100 files, setting pagesize = 20 and page = 2, will return files 40-60.

System Calls

System calls are intended to get server capabilities, list of method it exposes, method signatures, gettting, setting and deleting system varibles.

C#
array system.methodSignature(string method)

Returns requested method signature. Method is specified by method argument, which is method name. Returned value is an array of strings, which are names of types of method arguments.

Returns server capabilities, as members of struct. Member names are names of capabilities. Member values are structures with specUrl and specVersion members. Example of capabilities are: xmlrpc, system.multicall, introspection, json-rpc.

Enumerates the methods implemented by XML-RPC server. Method not takes input arguments. It returns an array of strings, each of which is the name of a method implemented by server.

Returns method's help string. Method name is specified by method argument, which is the string with requested method name.

Returns the details of current logged in user and session (sessid and session_name). The returned structure is the same as with user.login method.

Returns the value of a system variable using variable_get().

Sets the value of a system variable using variable_set().

Deletes a system variable using variable_del().

  • struct system.getCapabilities()
  • array system.listMethods()
  • string system.methodHelp(string method)
  • struct system.connect()
  • string system.get_variable(string name, string default)
  • system.set_variable(string name, string value)
  • system.del_variable(string name)

Summary

I described, the principles of xml-rpc, how it works with Drupal and what actions can be performed with Drupal using remote methods. There are also blog, taxonomy and other methods, but they are not in scope of this article. Also, there are other remote procedure protocols, supported by services module.

Please, send me your comments and suggestions.

Links

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)