类文档

IXR_Request

💡 云策文档标注

概述

IXR_Request 是 WordPress 中用于处理 XML-RPC 请求的类,提供构造请求、获取 XML 内容和长度的方法。它支持 PHP4 和 PHP5 构造函数,并生成符合 XML-RPC 标准的 XML 数据。

关键要点

  • IXR_Request 类用于创建和管理 XML-RPC 请求,包含 method、args 和 xml 属性。
  • 提供 __construct() 和 IXR_Request() 构造函数,分别用于 PHP5 和 PHP4 兼容性。
  • 方法 getLength() 返回 XML 字符串的长度,getXml() 返回生成的 XML 内容。
  • 在构造函数中,使用 IXR_Value 类处理参数,构建完整的 XML 请求体。

代码示例

class IXR_Request
{
    var $method;
    var $args;
    var $xml;

    function __construct($method, $args)
    {
        $this->method = $method;
        $this->args = $args;
        $this->xml = <<<EOD
{$this->method}
EOD;
        foreach ($this->args as $arg) {
            $this->xml .= '';
            $v = new IXR_Value($arg);
            $this->xml .= $v->getXml();
            $this->xml .= "n";
        }
        $this->xml .= '';
    }

    public function IXR_Request( $method, $args ) {
        self::__construct( $method, $args );
    }

    function getLength()
    {
        return strlen($this->xml);
    }

    function getXml()
    {
        return $this->xml;
    }
}

注意事项

  • IXR_Request 类自 WordPress 1.5.0 版本引入,用于 XML-RPC 通信。
  • 构造函数中的 IXR_Value 类用于将参数转换为 XML 格式,确保请求符合标准。
  • PHP4 构造函数 IXR_Request() 调用 PHP5 构造函数 __construct() 以保持向后兼容。

📄 原文内容

IXR_Request

Methods

Name Description
IXR_Request::__construct PHP5 constructor.
IXR_Request::getLength
IXR_Request::getXml
IXR_Request::IXR_Request PHP4 constructor.

Source

class IXR_Request
{
    var $method;
    var $args;
    var $xml;

	/**
	 * PHP5 constructor.
	 */
    function __construct($method, $args)
    {
        $this->method = $method;
        $this->args = $args;
        $this->xml = <<<EOD
<?xml version="1.0"?>
<methodCall>
<methodName>{$this->method}</methodname>
<params>

EOD;
        foreach ($this->args as $arg) {
            $this->xml .= '<param><value>';
            $v = new IXR_Value($arg);
            $this->xml .= $v->getXml();
            $this->xml .= "</value></param>n";
        }
        $this->xml .= '</params></methodcall>';
    }

	/**
	 * PHP4 constructor.
	 */
	public function IXR_Request( $method, $args ) {
		self::__construct( $method, $args );
	}

    function getLength()
    {
        return strlen($this->xml);
    }

    function getXml()
    {
        return $this->xml;
    }
}

Changelog

Version Description
1.5.0 Introduced.