Windows live writer’s “Invalid response document” error
In my previous post, I outlined my unsuccessful attempt to use windows live writer (WLW) to connect to the XmlRpc server of PhpWebsite‘s article manager module. I experimented using both the Metaweblog API and the Movable Type API but with no success. When attempting to post an entry, WLW always returned the following error message “The response to the metaWeblog.newPost method received from the weblog server was invalid: Invalid response document returned from XmlRpc server.” However, the entry did get posted. Republishing the same entry would create a new post instead of replacing the one posted earlier. And each time, WLW gave the same error.
I searched the Internet for possible solutions. I found out that the problem is quite common and not unique to PhpWebsite (PWS). The same problem has also been observed in other blogging platforms such as wordpress. Luckily for wordpress users, there are already solutions posted in several blogs for some of the problems with WLW. Unfortunately for me, the solutions were not applicable for PWS. I could not even find other sites or blogs talking about PWS and WLW except this one. It seems I am the only soul trying to do this in the entire world wide web.
Since most of the websites I’m maintaining are powered by PWS, I thought it is worth the time and effort to figure out the cause of the problem. I am also in need of a good editing application that I can use with PWS. The provided text editor is only good for basic editing and not for more advanced formatting. For instance, using tables in entries can really take a lot of time. Anyway, since I am using WLW in my other blogs, I thought it would be great if I could also use it in my PWS-based websites.
And so I started investigating (debugging). First, I checked the data returned by the XmlRpc server. I inserted a code in the server’s source file to write its output to a file. By comparing the written output with the specification available on the Internet, I confirmed that the error did not originate from the XmlRpc server since the output was according to specification. So I wondered what WLW was complaining about. Well, unless something happened to the returned data before reaching WLW. I started looking for ways to know the actual data received by WLW. Fortunately, I discovered this tool called fiddler, an HTTP debugging tool that logs all HTTP traffic between your computer and the Internet. Fiddler is a freeware and can be used to debug HTTP traffic.
Using fiddler to monitor the traffic between WLW and the HTTP server where the PWS XmlRpc server is running, I posted an entry to PWS. As expected, WLW gave the same error. And this is what I got from fiddler’s log:
Warning: Cannot modify header information - headers already sent by (output started at D:phpwebsitemodarticleincvalidate_login.php:62) in D:phpwebsitemodarticlelibXML-RPCIXR_Library.inc.php on line 366 Warning: Cannot modify header information - headers already sent by (output started at D:phpwebsitemodarticleincvalidate_login.php:62) in D:phpwebsitemodarticlelibXML-RPCIXR_Library.inc.php on line 367 Warning: Cannot modify header information - headers already sent by (output started at D:phpwebsitemodarticleincvalidate_login.php:62) in D:phpwebsitemodarticlelibXML-RPCIXR_Library.inc.php on line 368 Warning: Cannot modify header information - headers already sent by (output started at D:phpwebsitemodarticleincvalidate_login.php:62) in D:phpwebsitemodarticlelibXML-RPCIXR_Library.inc.php on line 369 <?xml version="1.0"?> <methodResponse> <params> <param> <value> <int>4</int> </value> </param> </params> </methodResponse>
The HTTP server returned a series of PHP warnings together with the xml document expected by WLW! Since WLW did not recognize the warnings and could not just extract the xml part of the returned document, it threw an invalid response document error! So there it is, the source of all my troubles!
Using the above log, it is also possible to know the cause of the problem. As it turned out, the validate_login.php file contained several line breaks after the closing php tag “?>” causing the HTTP server to start sending the returned document when the file was included. A call to the header function by the XmlRpc server generated the warnings, which together with the xml output were sent back by the HTTP server to WLW.
After fixing the validate_login.php file, I still encountered several “invalid response document” errors mostly due to PHP warnings. For instance, some loops using foreach returned warnings when the provided argument was not valid. Fortunately, it was a lot easier locating the source of the problem using fiddler’s log. After fixing the code, I can now successfully post entries to my PWS-based website using WLW. There are still many things that can be improved, but the important thing is the setup is already working.
So the next time you encounter this problem and you are sure that your XmlRpc server is working fine, check those included files for extra line breaks before the PHP opening tag “<?” or after the closing tag “?>”. Those “invalid document response” errors could just be due to those extra line breaks. Also verify whether PHP warnings are suppressed or not. Somewhere in your code some functions maybe returning warnings when the supplied parameters are not properly validated. Or if you cannot pinpoint the problem, try using fiddler to log the traffic between WLW and your XmlRpc server. Then check the data being sent back and forth for clues. Finally, for WLW developers, it would also be nice if WLW has a capability to display the raw data it receives from the XmlRpc server if the user wishes to view it. In this way, it will be a lot easier to debug problems encountered when configuring WLW.