views:

901

answers:

4

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!

A: 

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.

Novaktually
+1  A: 

@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.

Magsol
Glad you figured this out!
Novaktually
A: 

i receive this error too..

look:

object(xmlrpcresp)[74] public 'errno' => int 5 public 'errstr' => string 'Connect error: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution (0)'

i think it's a problem of connection (dns, ip address, hostname)

Nei Rauni Santos

A: 

hi guys, 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