PHP Script for XML-RPC calls from outside Drupal

thompcha's picture

Here is a set of PHP functions I put together for calling Drupal's core XML-RPC methods without using the Services API:

<?php
function assemble_request($method, $args = array(), $dump_request = FALSE){
       
$request = xmlrpc_encode_request($method,$args);
       
$context = stream_context_create(array('http' => array(
               
'method' => "POST",
               
'header' => "Content-Type: text/xml",
               
'content' => $request
       
)));
        if(
$dump_request){
                echo
'<pre>';
                echo
htmlentities($request);
                echo
'</pre>';
        }
        return
$context;
}

function
dump_response($response){
       echo
'<pre>';
                if (
xmlrpc_is_fault($response)) {
                       
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
                } else {
                       
print_r($response);
                }
       echo
'</pre>';
}

function
make_args_array($method, $session, $args = array(), $dump_args = FALSE){
       
$domain = 'localhost';
       
$timestamp = (string) time();
       
$nonce = user_password();
       
$key = '4a13889853ed635941670fe6054a9759';
       
$hash = hash_hmac('sha256', $timestamp .';'.$domain .';'. $nonce .';'.$method, $key);
       
$arrHash = array();
       
array_push($arrHash, $hash, $domain, $timestamp, $nonce, $session);
        if(
$dump_args){
                echo
'<pre>';
               
var_dump(array_merge($arrHash,$args));
                echo
'</pre>';
        }
        return
array_merge($arrHash,$args);
}

function
user_password($length = 10) {
       
$allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
       
$len = strlen($allowable_characters) - 1;
       
$pass = '';
        for (
$i = 0; $i < $length; $i++) {
               
$pass .= $allowable_characters[mt_rand(0, $len)];
        }
        return
$pass;
}
?>

Here is an example use that logs in a user and returns a node:

<?php
$ctxtSysConnect
= assemble_request("system.connect", NULL);
$file = file_get_contents("http://localhost/drupal/xmlrpc.php", false, $ctxtSysConnect);
$response = xmlrpc_decode($file);
$session = $response[sessid];

$arrArgs = make_args_array('user.login',$session,array("username","password"));
$ctxtSysConnect = assemble_request('user.login', $arrArgs);
$file = file_get_contents("http://localhost/drupal/xmlrpc.php", false, $ctxtSysConnect);
$response = xmlrpc_decode($file);
$session = $response[sessid];

$arrArgs = make_args_array('node.view',$session,array(11,array("field_date")),TRUE);
$ctxtSysConnect = assemble_request('node.view', $arrArgs);
$file = file_get_contents("http://localhost/drupal/xmlrpc.php", false, $ctxtSysConnect);
$response = xmlrpc_decode($file);
dump_response($response);
?>

If you're interested in working on making CCK date fields work with Drupal's "node.save" function, get in touch with me through my Drupal Groups user page.

Groups:
Login to post comments

I am using Fedora 10, PHP

nhchau's picture
nhchau - Mon, 2009-04-27 09:56

I am using Fedora 10, PHP 5.2.6. However my PHP does not understand xmlrpc_encode_request(). It said: "PHP Fatal error: Call to undefined function xmlrpc_encode_request() in /home/chau/public_html/tmp/rpc.php on line 5".
On the other side, php.net says xmlrpc_encode_request() is still in experimental stage (http://vn.php.net/xmlrpc_encode_request). Any other way to call XML-RPC from outside Drupal to Drupal? Thanks.