IXR_Server
云策文档标注
概述
IXR_Server 是 WordPress 中用于处理 XML-RPC 请求的核心类,提供服务器端功能,包括方法调用、错误处理和响应输出。它支持 PHP4 和 PHP5 构造函数,并内置了系统方法如 getCapabilities 和 listMethods。
关键要点
- IXR_Server 类用于实现 XML-RPC 服务器,处理 POST 请求并解析 XML-RPC 消息。
- 主要方法包括 serve() 用于处理请求、call() 用于执行回调、error() 用于错误处理、output() 用于输出响应。
- 支持设置回调函数和功能列表,通过 setCallbacks() 和 setCapabilities() 方法初始化。
- 包含系统方法如 system.multicall 用于批量调用,并防止递归调用。
- 与 IXR_IntrospectionServer 和 wp_xmlrpc_server 相关,用于扩展 XML-RPC 功能。
代码示例
// 示例:创建 IXR_Server 实例并处理请求
$server = new IXR_Server($callbacks, $data, $wait);
// $callbacks 为回调数组,$data 为可选输入数据,$wait 控制是否立即服务注意事项
- IXR_Server 仅接受 POST 请求,否则返回 405 状态码。
- 错误处理使用 IXR_Error 类,可通过 error() 方法输出错误 XML。
- 在 WordPress 环境中,输出时会考虑博客字符集设置。
原文内容
Methods
| Name | Description |
|---|---|
| IXR_Server::__construct | PHP5 constructor. |
| IXR_Server::call | – |
| IXR_Server::error | – |
| IXR_Server::getCapabilities | – |
| IXR_Server::hasMethod | – |
| IXR_Server::IXR_Server | PHP4 constructor. |
| IXR_Server::listMethods | – |
| IXR_Server::multiCall | – |
| IXR_Server::output | – |
| IXR_Server::serve | – |
| IXR_Server::setCallbacks | – |
| IXR_Server::setCapabilities | – |
Source
class IXR_Server
{
var $data;
var $callbacks = array();
var $message;
var $capabilities;
/**
* PHP5 constructor.
*/
function __construct( $callbacks = false, $data = false, $wait = false )
{
$this->setCapabilities();
if ($callbacks) {
$this->callbacks = $callbacks;
}
$this->setCallbacks();
if (!$wait) {
$this->serve($data);
}
}
/**
* PHP4 constructor.
*/
public function IXR_Server( $callbacks = false, $data = false, $wait = false ) {
self::__construct( $callbacks, $data, $wait );
}
function serve($data = false)
{
if (!$data) {
if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] !== 'POST') {
if ( function_exists( 'status_header' ) ) {
status_header( 405 ); // WP #20986
header( 'Allow: POST' );
}
header('Content-Type: text/plain'); // merged from WP #9093
die('XML-RPC server accepts POST requests only.');
}
$data = file_get_contents('php://input');
}
$this->message = new IXR_Message($data);
if (!$this->message->parse()) {
$this->error(-32700, 'parse error. not well formed');
}
if ($this->message->messageType != 'methodCall') {
$this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall');
}
$result = $this->call($this->message->methodName, $this->message->params);
// Is the result an error?
if (is_a($result, 'IXR_Error')) {
$this->error($result);
}
// Encode the result
$r = new IXR_Value($result);
$resultxml = $r->getXml();
// Create the XML
$xml = <<<EOD
<methodResponse>
<params>
<param>
<value>
$resultxml
</value>
</param>
</params>
</methodresponse>
EOD;
// Send it
$this->output($xml);
}
function call($methodname, $args)
{
if (!$this->hasMethod($methodname)) {
return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.');
}
$method = $this->callbacks[$methodname];
// Perform the callback and send the response
if (count($args) == 1) {
// If only one parameter just send that instead of the whole array
$args = $args[0];
}
// Are we dealing with a function or a method?
if (is_string($method) && substr($method, 0, 5) == 'this:') {
// It's a class method - check it exists
$method = substr($method, 5);
if (!method_exists($this, $method)) {
return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not exist.');
}
//Call the method
$result = $this->$method($args);
} else {
// It's a function - does it exist?
if (is_array($method)) {
if (!is_callable(array($method[0], $method[1]))) {
return new IXR_Error(-32601, 'server error. requested object method "'.$method[1].'" does not exist.');
}
} else if (!function_exists($method)) {
return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.');
}
// Call the function
$result = call_user_func($method, $args);
}
return $result;
}
function error($error, $message = false)
{
// Accepts either an error object or an error code and message
if ($message && !is_object($error)) {
$error = new IXR_Error($error, $message);
}
$this->output($error->getXml());
}
function output($xml)
{
$charset = function_exists('get_option') ? get_option('blog_charset') : '';
if ($charset)
$xml = ''."n".$xml;
else
$xml = ''."n".$xml;
$length = strlen($xml);
header('Connection: close');
if ($charset)
header('Content-Type: text/xml; charset='.$charset);
else
header('Content-Type: text/xml');
header('Date: '.gmdate('r'));
echo $xml;
exit;
}
function hasMethod($method)
{
return in_array($method, array_keys($this->callbacks));
}
function setCapabilities()
{
// Initialises capabilities array
$this->capabilities = array(
'xmlrpc' => array(
'specUrl' => 'https://xmlrpc.com/spec.md',
'specVersion' => 1
),
'faults_interop' => array(
'specUrl' => 'https://web.archive.org/web/20240416231938/https://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
'specVersion' => 20010516
),
'system.multicall' => array(
'specUrl' => 'https://web.archive.org/web/20060624230303/http://www.xmlrpc.com/discuss/msgReader$1208?mode=topic',
'specVersion' => 1
),
);
}
function getCapabilities($args)
{
return $this->capabilities;
}
function setCallbacks()
{
$this->callbacks['system.getCapabilities'] = 'this:getCapabilities';
$this->callbacks['system.listMethods'] = 'this:listMethods';
$this->callbacks['system.multicall'] = 'this:multiCall';
}
function listMethods($args)
{
// Returns a list of methods - uses array_reverse to ensure user defined
// methods are listed before server defined methods
return array_reverse(array_keys($this->callbacks));
}
function multiCall($methodcalls)
{
// See http://www.xmlrpc.com/discuss/msgReader$1208
$return = array();
foreach ($methodcalls as $call) {
$method = $call['methodName'];
$params = $call['params'];
if ($method == 'system.multicall') {
$result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden');
} else {
$result = $this->call($method, $params);
}
if (is_a($result, 'IXR_Error')) {
$return[] = array(
'faultCode' => $result->code,
'faultString' => $result->message
);
} else {
$return[] = array($result);
}
}
return $return;
}
}
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |