正如在XML-RPC客户端例子中所看到的,Drupal为你做了大部分工作。现在让我们看一个简单的服务器端的例子。你需要做3件事来建立你的服务器:
1, 定义一个当客户请求到来时你想执行的函数。
2, 将函数映射为一个公共的方法名。
3, 可选的定义一个方法签名。
按照Drupal的惯例,你想将你的代码与系统核心分开并将其作为一个模块插入。下面是一个简单的模块,用来通过XML-RPC说”你好”.创建文件sites/all/modules/custom/remotehello/remotehello.info:
; $Id$
name = Remote Hello
description = Greets XML-RPC clients by name.
package = Pro Drupal Development
core = 6.x
下面是 remotehello.module:
<?php
// $Id$
/**
* Implementation of hook_xmlrpc().
* Map external names of XML-RPC methods to PHP callback functions.
*/
function remotehello_xmlrpc() {
$methods['remoteHello.hello'] = 'xmls_remotehello_hello';
return $methods;
}
/**
* Greet a user.
*/
function xmls_remotehello_hello($name) {
if (!$name) {
return xmlrpc_error(1, t('I cannot greet you by name if you do not
provide one.'));
}
return t('Hello, @name!', array('@name' => $name));
}
老葛的Drupal培训班 Think in Drupal