WordPress XMLRPC:Expat 报告错误代码 5
几个月前,我编写了一个小型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我修复了在 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
@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.
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 afterfile_get_contents
and beforexmlrpc_decode
.