XML Parser Error Code 5 with WordPress XML-RPC Integration

I built a PHP script a few months back that connects two WordPress sites using the XML-RPC API. Everything was working great until recently when I started getting a weird error.

I have a custom class that handles the XML-RPC communication and I’m trying to fetch post information from a remote WordPress site. Here’s my simplified code:

$client = new WPSyncClient('http://example-site.com/xmlrpc.php', 'myuser', 'mypass');
$client->fetchPostList();

But now I’m getting this error:

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:

The weird part is that if I pass any number as a parameter like fetchPostList(5), it works perfectly. Here’s my class structure:

class WPSyncClient {
    public function __construct($endpoint, $user, $pass) {
        $this->endpoint = $endpoint;
        $this->user = $user;
        $this->pass = $pass;
        
        $userInfo = $this->makeRequest("blogger.getUserInfo", 
                                     array("null", $this->user, $this->pass));
        $this->siteID = $userInfo['userid'];
    }
    
    public function makeRequest($method, $args) {
        $requestData = xmlrpc_encode_request($method, $args);
        $httpContext = stream_context_create(array('http' => array(
                        'method' => "POST",
                        'header' => "Content-Type: text/xml",
                        'content' => $requestData
        )));
        
        $response = file_get_contents($this->endpoint, false, $httpContext);
        return xmlrpc_decode($response);
    }
    
    public function fetchPostList($limit = 0) {
        return $this->makeRequest("mt.getRecentPostTitles",
                                array($this->siteID, $this->user, 
                                $this->pass, $limit));
    }
}

This used to work fine when I passed 0 as the default parameter (which tells the server to return all posts). The error only started happening recently and I can’t figure out what changed.

What does this expat error code 5 actually mean and where is it coming from? Is it from my local XML parser or the remote server?

sounds like a server issue, not ur code. the remote WP site prob changed their PHP config or hit resource limits. add error_reporting to see what’s actually coming back - you’re likely getting an HTML error page instead of XML when limit=0 causes memory exhaustion.

This expat error means the XML-RPC response is empty or broken. I’ve seen this with older WordPress sites where the server times out on big requests. When you set limit=5, the server handles it fine. But limit=0 (all posts) can crash the server if your site has tons of content - it hits memory limits or timeouts before generating the XML response. You get an empty response that the XML parser can’t read. First, check your remote server’s error logs - you’ll probably find PHP fatal errors or timeouts when the unlimited request runs. Also check if the remote WordPress site updated recently. Newer versions have stricter resource limits that can cause this on large requests.

Error code 5 means your XML parser is getting bad or incomplete XML from the remote server. Since this just started happening without any code changes, I’m betting the remote WordPress site either updated or now has enough posts to cause performance problems. When you call fetchPostList() without parameters, that default limit=0 tells WordPress to return ALL posts. That’s brutal on resources. The remote server probably starts processing your request but runs out of memory or hits timeout limits before finishing the XML response. You end up with cut-off or empty output that breaks the expat parser. I’d add some debugging to your makeRequest method to see what’s actually coming back. Throw a var_dump($response) before the xmlrpc_decode line. You’ll probably find either an empty string or a partial HTML error page instead of proper XML. Fix is usually setting a reasonable default limit instead of 0, or adding pagination for large datasets.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.