WordPress XMLRPC:Expat 报告错误代码 5

发布于 2024-07-04 18:11:44 字数 1998 浏览 10 评论 0原文

几个月前,我编写了一个小型 PHP 应用程序,它使用 WordPress XMLRPC 库 来同步两个单独的 WordPress 博客。 我有一个通用的“RPCRequest”函数,用于打包请求、发送请求并返回服务器响应,并且我有几个更具体的函数,用于自定义发送的请求类型。

在这种特殊情况下,我调用“getPostIDs”来检索远程服务器上的帖子数量及其各自的帖子 ID。 这是代码:

$rpc = new WordRPC('http://mywordpressurl.com/xmlrpc.php', 'username', 'password');
$rpc->getPostIDs();

我收到以下错误消息:

expat reports error code 5
description: Invalid document end
line: 1
column: 1
byte index: 0
total bytes: 0

data beginning 0 before byte index: 

有点悬念的结局,这也很奇怪。 但由于错误消息不是 XML 格式的,因此我的直觉是生成错误的是本地 XMLRPC 库,而不是远程服务器。

更奇怪的是,如果我将“getPostIDs()”调用更改为“getPostIDs(1)”或任何其他整数,它就可以正常工作。

下面是 WordRPC 类的代码:

public function __construct($url, $user, $pass) {
  $this->url = $url;
  $this->username = $user;
  $this->password = $pass;

  $id = $this->RPCRequest("blogger.getUserInfo",
                          array("null", $this->username, $this->password));
  $this->blogID = $id['userid'];
}

public function RPCRequest($method, $params) {
  $request = xmlrpc_encode_request($method, $params);
  $context = stream_context_create(array('http' => array(
                    'method' => "POST",
                    'header' => "Content-Type: text/xml",
                    'content' => $request
  )));

  $file = file_get_contents($this->url, false, $context);
  return xmlrpc_decode($file);
}

public function getPostIDs($num_posts = 0) {
  return $this->RPCRequest("mt.getRecentPostTitles",
                            array($this->blogID, $this->username,
                            $this->password, $num_posts));
}

正如我所提到的,如果给“getPostIDs”一个正整数参数,它就可以正常工作。 此外,这曾经可以很好地工作; 默认参数 0 只是指示 RPC 服务器应该检索所有 个帖子,而不仅仅是最近的 $num_posts 个帖子。 直到最近这个错误才开始出现。

我尝试用谷歌搜索该错误,但运气不佳。 那么,我的问题是“外籍人士报告错误代码 5”到底是什么意思,以及谁生成了错误?除此之外的任何详细信息/建议/见解也欢迎!

I wrote a small PHP application several months ago that uses the WordPress XMLRPC library to synchronize two separate WordPress blogs. I have a general "RPCRequest" function that packages the request, sends it, and returns the server response, and I have several more specific functions that customize the type of request that is sent.

In this particular case, I am calling "getPostIDs" to retrieve the number of posts on the remote server and their respective postids. Here is the code:

$rpc = new WordRPC('http://mywordpressurl.com/xmlrpc.php', 'username', 'password');
$rpc->getPostIDs();

I'm receiving the following error message:

expat reports error code 5
description: Invalid document end
line: 1
column: 1
byte index: 0
total bytes: 0

data beginning 0 before byte index: 

Kind of a cliffhanger ending, which is also strange. But since the error message isn't formatted in XML, my intuition is that it's the local XMLRPC library that is generating the error, not the remote server.

Even stranger, if I change the "getPostIDs()" call to "getPostIDs(1)" or any other integer, it works just fine.

Here is the code for the WordRPC class:

public function __construct($url, $user, $pass) {
  $this->url = $url;
  $this->username = $user;
  $this->password = $pass;

  $id = $this->RPCRequest("blogger.getUserInfo",
                          array("null", $this->username, $this->password));
  $this->blogID = $id['userid'];
}

public function RPCRequest($method, $params) {
  $request = xmlrpc_encode_request($method, $params);
  $context = stream_context_create(array('http' => array(
                    'method' => "POST",
                    'header' => "Content-Type: text/xml",
                    'content' => $request
  )));

  $file = file_get_contents($this->url, false, $context);
  return xmlrpc_decode($file);
}

public function getPostIDs($num_posts = 0) {
  return $this->RPCRequest("mt.getRecentPostTitles",
                            array($this->blogID, $this->username,
                            $this->password, $num_posts));
}

As I mentioned, it works fine if "getPostIDs" is given a positive integer argument. Furthermore, this used to work perfectly well as is; the default parameter of 0 simply indicates to the RPC server that it should retrieve all posts, not just the most recent $num_posts posts. Only recently has this error started showing up.

I've tried googling the error without much luck. My question, then, is what exactly does "expat reports error code 5" mean, and who is generating the error? Any details/suggestions/insights beyond that are welcome, too!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

伊面 2024-07-11 18:11:44

我修复了在 apache 上安装 php-xmlrpc 模块时出现的错误

php-xmlrpc.x86_64 :使用 XML-RPC 协议的 PHP 应用程序的模块

i fixed this error installing php-xmlrpc module on apache

php-xmlrpc.x86_64 : A module for PHP applications which use the XML-RPC protocol

树深时见影 2024-07-11 18:11:44

@Novak:感谢您的建议。 结果发现问题是内存问题; 通过从远程位置检索所有帖子,响应超出了允许 PHP 使用的内存量,因此出现未关闭的令牌错误。

出现神秘且不完整的错误消息的问题是由于使用了过时版本的 XML-RPC 库造成的。 当我升级 WordPress 版本后,它为我提供了完整的错误输出,包括内存错误。

@Novak: Thanks for your suggestion. The problem turned out to be a memory issue; by retrieving all the posts from the remote location, the response exceeded the amount of memory PHP was allowed to utilize, hence the unclosed token error.

The problem with the cryptic and incomplete error message was due to an outdated version of the XML-RPC library being used. Once I'd upgraded the version of WordPress, it provided me with the complete error output, including the memory error.

猛虎独行 2024-07-11 18:11:44

Expat 是 PHP 中的 XML 解析器。 错误代码 5 是许多 expat 错误常量之一,在本例中为:XML_ERROR_UNCLOSED_TOKEN。 在我看来,RPC 调用返回的结果有错误。 您可能希望在 file_get_contents 之后和 xmlrpc_decode 之前在 RPCRequest 中进行一些错误检查。

Expat is the XML parser in PHP. Error code 5 is one of many expat error constants, in this case: XML_ERROR_UNCLOSED_TOKEN. Sounds to me like there's an error in the result returned from the RPC call. You might want to do some error checking in RPCRequest after file_get_contents and before xmlrpc_decode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文