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 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:

    1. require_once("xmlrpc/xmlrpc.inc");


    In the class constructor, we establish the connection:
    1. function __construct() {
    2.   $this->send_message('system.connect');
    3. }


    And accordingly, it would be nice to register sending messages:

    1. function send_message($method, $message = array()) {
    2.    $XMLRPC = new xmlrpc_client(_PATH, _SERV, _PORT);
    3.    $XMLRPC ->return_type = "phpvals";
    4.  
    5.    $msg = new xmlrpcmsg($method, $message);
    6.    $ret = $XMLRPC->send($msg);
    7.  
    8.    if(!$ret->faultCode()) {
    9.      $answer = $ret->value();
    10.      if (isset($answer['sessid']) && !isset(self::$session)) {
    11.        self::$session = $answer['sessid'];
    12.      }
    13.      return $ret->value();
    14.    } else return $ret->faultString();
    15. }


    Now add the node. To do this, pass the username and message to send as parameters:

    1. function node_save($user, $message) {
    2.  $node = array (
    3.   'title' => '*',
    4.   'body' => $message,
    5.   'type' => 'blog',
    6.   'promote' => 1,
    7.   'uid' => 0 ,
    8.   'name' => 'Anonymouse' ,
    9.  );
    10.  $msg = array(
    11.   php_xmlrpc_encode( self::$session ),
    12.   php_xmlrpc_encode( $node )
    13.  );
    14.  return $this->send_message('node.save', $msg);
    15. }


    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.

    Also popular now: