In this article I will tell how you can use this wonderful technology in a no less remarkable Drupal system. As an example, we will try to make a system that allows you to send messages to sites managed by Drupal from your jabber client.
To save time, apply the following tools:
- Drupal 6
- Services (module for Drupal 6)
- xmppphp - to create a jabber bot (for a test application would be fine)
- xmlrpc - simplify your development
- jid - under which the bot will work
- small auxiliary class
DrupalInstall Drupal. After installation, we include the Blog module. Subsequent messages will be posted in the blogs of users as follows. The person adds the bot to his roster, then sends the bot a message. Bot looks: is there such a user (with the same email address)? If so, it adds a message on behalf of this user. If there are none, then register a new user and add a message on his behalf.
Drupal lives at drupal.org')
ServicesIt should be noted that on the module page, at the time of this writing, it is recommended to use development snapshot. And install it. After installation, we include everything related to services. The module has various authentication capabilities. I leave the choice of the 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/ServicesJabber botWith this item questions will be the least, because we take the bot from the example. We take any console bot (there are already two of them there) and add a few lines that will include our auxiliary class.
The project is conveniently located on the Google code, and you can find it
this link:
code.google.com/p/xmpphpDownload, unpack, run the bot, using jid for authorization.
Start the danceDrupal is ready, services are running, the bot is warmly waiting for new messages. Now the turn has come to programming. We will write several functions that will help simplify the task a bit.
Create a small class and here we need the xmlrpc library:
require_once( "xmlrpc/xmlrpc.inc" );
In the class constructor, 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, as a parameter, pass the user name and message to send:
- 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, is all that was needed to be done. Perhaps the observant habrouser will notice that the $ user parameter was not in any way involved. I decided to leave work with users on the conscience of readers. But if someone wants to quickly fasten something similar to his blog, then the whole class can be found here:
github.com/mcnet/drupal.xmlrpc.classGood luck!
Threat I would not recommend using the class without some refinement. At the moment, all the code is presented as an introductory example.