Friday, December 27, 2013

#xmlrpc | Really understanding what it is

Problem:
I am trying to access a few blogs with APIs.
Some - like Blogger - are now moving to OAuth 2.0 and Google API although it seems to yet support older methods.
Others - like WordPress - use the XML-RPC API.
As any other API, there are some people over the great Internet that create client libraries, which are pieces of code that we can include on our projects that take care of the whole API connection stuff, like authentication, data structures, and others.
One of the best client libraries is XML-RPC.NET.
So, we have PHP client libraries, and we have ASP.NET client libraries. Then what is the problem?

The problem is that we don't really know what XML-RPC is or weather or not we actually need a library in order to accomplish something simple. Or, for example, these libraries don't yet support our platforms (Windows Phone 8, etc.).
This fellow asked the pretty basic question. How to I set up my own, basic, simple XML-RPC code?


Solution:
In order to solve the problem above, we need to understand a few things:

1) XML RPC is actually a server file that is listening to our API requests.
For WordPress we can find this file by adding "/xmlrpc.php" to our WordPress website URL.
E.g. http://tiagoduarte85.wordpress.com/xmlrpc.php
(you should see a message saying "XML-RPC server accepts POST requests only.")

2) The name says everything.
"All" we need to do is create a XML file (or XML markup) and then POST it to the URL above as an HTTP POST with HttpClient.

3) Let's test it
Run a REST Browser Client, choose "POST", type in your website URL with /xmlrpc.php and add the code below to the body area.
We are using the getPosts method. If you check the WordPress Codex, you will find that it takes 3 mandatory parameters (in the specified order): Blog ID, Username, Password.
We wrap those in a "methodCall", adding the "methodName" we want and that's it.

            <methodCall>
            <methodName>wp.getPosts</methodName>
            <params>
            <param>
            <value><int>1</int></value>
            </param>
            <param>
            <value><string>admin</string></value>
            </param>
            <param>
            <value><string>password</string></value>
            </param>
            </params>
            </methodCall>


No comments:

Post a Comment