Using XML-RPC in Drupal. Quickstart
In this article I will tell you how you can use this wonderful technology in the equally wonderful Drupal system. As an example, let's try to make a system that allows you to send messages to sites running Drupal from your jabber client.
To save time, we apply the following tools:
Drupal
Install Drupal. After installation, turn on the Blog module. We will post subsequent posts on users' blogs in the following way. A person adds a bot to his roster, then sends a message to the bot. The bot looks: is there such a user (with the same mailing address)? If there is, it adds a message on behalf of this user. If there are none, then registers a new user and add a message on his behalf.
Drupal lives at drupal.org
Services
It should be noted that on the module page, at the time of writing, it is recommended to use development snapshot. We will install it. After installation, we include everything related to services. The module has various authentication capabilities. I leave the choice of method to your taste, however, it should be noted that the example used authorization based on the user name and password.
The module is located here drupal.org/project/Services
Jabber Bot
C this item will be the least of questions, because we will take a bot from an example. We take any console bot (there are already two of them) and add a few lines that will include our auxiliary class.
The project is snugly located on Google code, and you can find it
at this link:code.google.com/p/xmpphp
Download, unpack, launch the bot using jid for authorization.
Start the dance
Drupal is ready, services are running, the bot is friendly waiting for new messages. Now the turn has come to programming. We will write a few functions that will help to simplify the task a bit.
Let's create a small class and here we need the xmlrpc library:
In the class constructor, we establish the connection:
And accordingly, it would be nice to register sending messages:
Now add the node. To do this, pass the username and message to send as parameters:
That, in general, was all that needed to be done. Perhaps the observational hub browser will notice that the $ user parameter was in no way involved. I decided to leave the work with users to the conscience of readers. But if someone wants to quickly fasten something similar to their blog, then the whole class can be found here: github.com/mcnet/drupal.xmlrpc.class
Good luck!
Threat I would not recommend using the class without some refinement. At the moment, all the code is presented as a fact-finding example.
To save time, we apply the following tools:
- Drupal 6
- Services (module for Drupal 6)
- xmppphp - to create a jabber bot (for a test application it will do)
- xmlrpc - simplify your development
- jid - under which the bot will work
- small helper class
Drupal
Install Drupal. After installation, turn on the Blog module. We will post subsequent posts on users' blogs in the following way. A person adds a bot to his roster, then sends a message to the bot. The bot looks: is there such a user (with the same mailing address)? If there is, it adds a message on behalf of this user. If there are none, then registers a new user and add a message on his behalf.
Drupal lives at drupal.org
Services
It should be noted that on the module page, at the time of writing, it is recommended to use development snapshot. We will install it. After installation, we include everything related to services. The module has various authentication capabilities. I leave the choice of method to your taste, however, it should be noted that the example used authorization based on the user name and password.
The module is located here drupal.org/project/Services
Jabber Bot
C this item will be the least of questions, because we will take a bot from an example. We take any console bot (there are already two of them) and add a few lines that will include our auxiliary class.
The project is snugly located on Google code, and you can find it
at this link:code.google.com/p/xmpphp
Download, unpack, launch the bot using jid for authorization.
Start the dance
Drupal is ready, services are running, the bot is friendly waiting for new messages. Now the turn has come to programming. We will write a few functions that will help to simplify the task a bit.
Let's create a small class and here we need the xmlrpc library:
- require_once("xmlrpc/xmlrpc.inc");
In the class constructor, we establish the connection:
- function __construct() {
- $this->send_message('system.connect');
- }
And accordingly, it would be nice to register sending messages:
- function send_message($method, $message = array()) {
- $XMLRPC = new xmlrpc_client(_PATH, _SERV, _PORT);
- $XMLRPC ->return_type = "phpvals";
-
- $msg = new xmlrpcmsg($method, $message);
- $ret = $XMLRPC->send($msg);
-
- if(!$ret->faultCode()) {
- $answer = $ret->value();
- if (isset($answer['sessid']) && !isset(self::$session)) {
- self::$session = $answer['sessid'];
- }
- return $ret->value();
- } else return $ret->faultString();
- }
Now add the node. To do this, pass the username and message to send as parameters:
- function node_save($user, $message) {
- $node = array (
- 'title' => '*',
- 'body' => $message,
- 'type' => 'blog',
- 'promote' => 1,
- 'uid' => 0 ,
- 'name' => 'Anonymouse' ,
- );
- $msg = array(
- php_xmlrpc_encode( self::$session ),
- php_xmlrpc_encode( $node )
- );
- return $this->send_message('node.save', $msg);
- }
That, in general, was all that needed to be done. Perhaps the observational hub browser will notice that the $ user parameter was in no way involved. I decided to leave the work with users to the conscience of readers. But if someone wants to quickly fasten something similar to their blog, then the whole class can be found here: github.com/mcnet/drupal.xmlrpc.class
Good luck!
Threat I would not recommend using the class without some refinement. At the moment, all the code is presented as a fact-finding example.