Drupal - Services PHP XML-RPC Example

drupal.org.png

So I read all I could about Drupal Services, I looked in the docs, I searched the group, I found no good info on a external to drupal PHP XML-RPC Client that connects to Drupal Services and interface with the node. Actually I found that the process of auth, node.load, and node.save are very poorly documented. So I am posting my findings.

I will update this as I investigate the search and other services.

Greg

Directions:
In Drupal turn on "Use sessid" - admin/build/services/settings

In your XML-RPC Clinet you will use the following methods: 1-2-3a or 1-2-3b or 1-2-3c
1. system.connect with no args
returns - session id

2. user.login
requires - session id from system.connect

3a. node.load
requires - session id from user.login
requires - nid
requires - array of fields, or empty array

3b. node.save (new node)
requires - session id from user.login
requires - type
requires - uid & name - if you want to associate to a user, otherwise it will be anonymous
optional - any other field you want to specify

3c. node.save (update node)
requires - session id from user.login
requires - type
requires - uid & name - if you want to associate to a user, otherwise it will be anonymous
required - revision - time()
required - created - time()
required - nid
optional - any other field you want to specify

Links:
Docs - http://drupal.org/handbook/modules/services
Group - http://groups.drupal.org/services
PHP XML-RPC Lib - http://phpxmlrpc.sourceforge.net

Here is the sample code to connect, auth, node.load, node.save (new node), & node.save (update node)
Of course you should not run all of this at once.

<?php
include("xmlrpc.inc");
function print_nice($i){
        echo "<pre>";
        print_r($i);
        echo "</pre>";
}

//connect to the system
//get the session id to use for the user.login
$m = new xmlrpcmsg('system.connect',
                array()
                );
$c = new xmlrpc_client("?q=services/xmlrpc", "localhost", 80);
$c->setDebug(0);
$c->return_type = "phpvals";
$r = &$c->send($m);
if(!$r->faultCode()){
        $v=$r->value();
        $sessid = ($v['sessid']);
}else{
        print "An error occurred on system.connect: ";
}

//login to the system
//use the session id from the system.connect
//get logged in session id for node.load
$m = new xmlrpcmsg('user.login',
                array(
                        php_xmlrpc_encode($sessid),
                        php_xmlrpc_encode("user1"),
                        php_xmlrpc_encode("p4ssw0rd"),
                        )
                );
$c = new xmlrpc_client("?q=services/xmlrpc", "localhost", 80);
$c->setDebug(0);
$c->return_type = "phpvals";
$r = &$c->send($m);
if(!$r->faultCode()){
        $v=$r->value();
        $sessid = ($v['sessid']);
        $uid = $v['user']['uid'];
}else{
        print "An error occurred on user.login: ";
}

// get the info from a node
// use the session id from the user.login
// get node information
$m = new xmlrpcmsg('node.load',
                array(
                        php_xmlrpc_encode($sessid),                     //session from user.login
                        php_xmlrpc_encode(10),                          //nid
                        php_xmlrpc_encode(array()),                     //empty array for all option, array('title') if you want the title
                        )
                );
$c = new xmlrpc_client("?q=services/xmlrpc", "localhost", 80);
$c->setDebug(0);
$c->return_type = "phpvals";
$r = &$c->send($m);
if(!$r->faultCode()){
        $v = $r->value();
        print_nice($v);
}else{
        print "An error occurred on node.load: ";
}

//save new node
//use the session id from the user.login
//save new info to a node
$change = array(
                "type" => "page",                               //required
                "uid" => 1,                                     //required both uid and name to associate to a user
                "name" => "user1",                              //required both uid and name to associate to a user

                "title" => "TEST NODE.SAVE TITLE",
                "body" => "this is a body",
        );

$m = new xmlrpcmsg('node.save',
                array(
                        php_xmlrpc_encode($sessid),
                        php_xmlrpc_encode($change)
                        )
                );
$c = new xmlrpc_client("?q=services/xmlrpc", "localhost", 80);
$c->setDebug(0);
$c->return_type = "phpvals";
$r = &$c->send($m);
if(!$r->faultCode()){
        $v = $r->value();
        print_nice($v);
}else{
        print "An error occurred on node.save: ";
}

//update a node
//use the session id from the user.login
//send new info to a node
$change = array(
                "nid" => 14,                                    //required for update
                "revision" => time(),                   //required for update
                "changed" => time(),                    //required for update
                "type" => "page",                               //required
                "uid" => 1,                                     //required both uid and name to associate to a user
                "name" => "user1",                              //required both uid and name to associate to a user

                "title" => "TEST NODE.SAVE TITLE **UPDATES**",
                "status" => 1,                                  // = published
                "body" => "this is a body **UPDATES**",
        );

print_nice ($change);
$m = new xmlrpcmsg('node.save',
                array(
                        php_xmlrpc_encode($sessid),
                        php_xmlrpc_encode($change)
                        )
                );
$c = new xmlrpc_client("?q=services/xmlrpc", "localhost", 80);
$c->setDebug(0);
$c->return_type = "phpvals";
$r = &$c->send($m);
if(!$r->faultCode()){
        $v = $r->value();
        print_nice($v);
}else{
        print "An error occurred on node.save: ";
}
?>

Comments

jordan shoes

1、We have excellent customer service team, which could solve online various problems about the Air Jordan we provide in 24 hours. 2、We have perfect Logistics system, which guarantees all the ordered Nike Air Jordan shoes are delivered to you in good shape as fast as possible. 3、We have strong manufacture plant, which are able to provide various models, sizes & colors of Air Jordan shoes ranging from 1 to 23 according to what you request. 4、We have the first-rate after-sale service , in case that the Jordan Shoes products that you receive come across some quality problems, then, please do not worry, we are abound to refund all the payment in one week,. Also, the products can be changed in one me month.The main products we specialize in are showed as follows
Our Jordan Shoes: Air Jordan 7 Air Jordan 8 Air Jordan 9 Air Jordan 10 Air Jordan 11 Air Jordan 12 Jordan 7 Jordan 8 Jordan 9 Jordan 10 Jordan 11 Jordan 12

thank you very much!

node.load is node.get now

Is this D5 or D6?

Does it even matter? Thanks for sharing the code! Mike