类文档

IXR_Client

💡 云策文档标注

概述

IXR_Client 是 WordPress 中用于 XML-RPC 通信的客户端类,支持 PHP4 和 PHP5 构造函数,提供发送查询、处理响应和错误管理的方法。

关键要点

  • 类 IXR_Client 包含服务器、端口、路径等属性,用于配置 XML-RPC 连接。
  • 主要方法包括 query() 用于发送 XML-RPC 请求,getResponse() 获取响应,isError() 检查错误,以及 getErrorCode() 和 getErrorMessage() 处理错误信息。
  • 支持自定义 HTTP 头部和调试模式,通过 fsockopen 进行网络通信。
  • 从 WordPress 1.5.0 版本引入,后续版本如 5.5.0 对 query() 方法的参数进行了正式化。

代码示例

// 示例:使用 IXR_Client 发送 XML-RPC 查询
$client = new IXR_Client('http://example.com', '/xmlrpc.php');
if ($client->query('methodName', $arg1, $arg2)) {
    $response = $client->getResponse();
    echo 'Response: ' . print_r($response, true);
} else {
    echo 'Error: ' . $client->getErrorMessage();
}

注意事项

  • 构造函数支持直接传入 URL 或分离的服务器、路径、端口参数,自动解析 URL 组件。
  • query() 方法使用 IXR_Request 构建请求,处理 HTTP 响应和 XML 解析,返回布尔值表示成功与否。
  • 错误处理通过 IXR_Error 对象实现,需检查 isError() 后获取具体错误代码和消息。
  • 相关类包括 IXR_ClientMulticall 和 WP_HTTP_IXR_Client,用于扩展功能。

📄 原文内容

IXR_Client

Methods

Name Description
IXR_Client::__construct PHP5 constructor.
IXR_Client::getErrorCode
IXR_Client::getErrorMessage
IXR_Client::getResponse
IXR_Client::isError
IXR_Client::IXR_Client PHP4 constructor.
IXR_Client::query

Source

class IXR_Client
{
    var $server;
    var $port;
    var $path;
    var $useragent;
    var $response;
    var $message = false;
    var $debug = false;
    var $timeout;
    var $headers = array();

    // Storage place for an error message
    var $error = false;

	/**
	 * PHP5 constructor.
	 */
    function __construct( $server, $path = false, $port = 80, $timeout = 15 )
    {
        if (!$path) {
            // Assume we have been given a URL instead
            $bits = parse_url($server);
            $this->server = $bits['host'];
            $this->port = isset($bits['port']) ? $bits['port'] : 80;
            $this->path = isset($bits['path']) ? $bits['path'] : '/';

            // Make absolutely sure we have a path
            if (!$this->path) {
                $this->path = '/';
            }

            if ( ! empty( $bits['query'] ) ) {
                $this->path .= '?' . $bits['query'];
            }
        } else {
            $this->server = $server;
            $this->path = $path;
            $this->port = $port;
        }
        $this->useragent = 'The Incutio XML-RPC PHP Library';
        $this->timeout = $timeout;
    }

	/**
	 * PHP4 constructor.
	 */
	public function IXR_Client( $server, $path = false, $port = 80, $timeout = 15 ) {
		self::__construct( $server, $path, $port, $timeout );
	}

	/**
	 * @since 1.5.0
	 * @since 5.5.0 Formalized the existing `...$args` parameter by adding it
	 *              to the function signature.
	 *
	 * @return bool
	 */
    function query( ...$args )
    {
        $method = array_shift($args);
        $request = new IXR_Request($method, $args);
        $length = $request->getLength();
        $xml = $request->getXml();
        $r = "rn";
        $request  = "POST {$this->path} HTTP/1.0$r";

        // Merged from WP #8145 - allow custom headers
        $this->headers['Host']          = $this->server;
        $this->headers['Content-Type']  = 'text/xml';
        $this->headers['User-Agent']    = $this->useragent;
        $this->headers['Content-Length']= $length;

        foreach( $this->headers as $header => $value ) {
            $request .= "{$header}: {$value}{$r}";
        }
        $request .= $r;

        $request .= $xml;

        // Now send the request
        if ($this->debug) {
            echo '<pre class="ixr_request">'.htmlspecialchars($request)."n</pre>nn";
        }

        if ($this->timeout) {
            $fp = @fsockopen($this->server, $this->port, $errno, $errstr, $this->timeout);
        } else {
            $fp = @fsockopen($this->server, $this->port, $errno, $errstr);
        }
        if (!$fp) {
            $this->error = new IXR_Error(-32300, 'transport error - could not open socket');
            return false;
        }
        fputs($fp, $request);
        $contents = '';
        $debugContents = '';
        $gotFirstLine = false;
        $gettingHeaders = true;
        while (!feof($fp)) {
            $line = fgets($fp, 4096);
            if (!$gotFirstLine) {
                // Check line for '200'
                if (strstr($line, '200') === false) {
                    $this->error = new IXR_Error(-32300, 'transport error - HTTP status code was not 200');
                    return false;
                }
                $gotFirstLine = true;
            }
            if (trim($line) == '') {
                $gettingHeaders = false;
            }
            if (!$gettingHeaders) {
            	// merged from WP #12559 - remove trim
                $contents .= $line;
            }
            if ($this->debug) {
            	$debugContents .= $line;
            }
        }
        if ($this->debug) {
            echo '<pre class="ixr_response">'.htmlspecialchars($debugContents)."n</pre>nn";
        }

        // Now parse what we've got back
        $this->message = new IXR_Message($contents);
        if (!$this->message->parse()) {
            // XML error
            $this->error = new IXR_Error(-32700, 'parse error. not well formed');
            return false;
        }

        // Is the message a fault?
        if ($this->message->messageType == 'fault') {
            $this->error = new IXR_Error($this->message->faultCode, $this->message->faultString);
            return false;
        }

        // Message must be OK
        return true;
    }

    function getResponse()
    {
        // methodResponses can only have one param - return that
        return $this->message->params[0];
    }

    function isError()
    {
        return (is_object($this->error));
    }

    function getErrorCode()
    {
        return $this->error->code;
    }

    function getErrorMessage()
    {
        return $this->error->message;
    }
}

Changelog

Version Description
1.5.0 Introduced.