MetaWeblog API Examples

Topics: Argotic.Common, Argotic.Core, Argotic.Extensions
Jan 15, 2008 at 5:30 PM
Does anyone have an example of how to implement XML-RPC specifically the MetaWeblog API using the Argotic framework?

I would like to publish articles to two of my blogs. One is running Joomla 1.5 RC4 and the other is running WordPress. Any help with this would be greatly appreciated. If I figure it out in the meantime I will post the code here but at the moment I am stuck.

Thanks.
Jan 15, 2008 at 10:55 PM
Edited Jan 15, 2008 at 11:04 PM


bbehm wrote:
Does anyone have an example of how to implement XML-RPC specifically the MetaWeblog API using the Argotic framework?

I would like to publish articles to two of my blogs. One is running Joomla 1.5 RC4 and the other is running WordPress. Any help with this would be greatly appreciated. If I figure it out in the meantime I will post the code here but at the moment I am stuck.

Thanks.


I am working with the metaWeblog.newPost message. The part that I am mainly stuck on is the "struct" parameter. As I understand, the parameters for this message are follows:

metaWeblog.newPost (blogid, username, password, struct, publish) returns string

The struct parameter should contain members for title, description, author, etc for the article that I am trying to post.

Here is a code sample based on how I should implement this with the XmlRpcClient and XmlRpcMessage classes:

                // Publish the message
                using (XmlRpcClient client = new XmlRpcClient())
                {
                    // Set the timeout value
                    client.Timeout = TimeSpan.FromSeconds(10);
 
                    // Create the message
                    XmlRpcMessage msg = new XmlRpcMessage();
                    msg.Encoding = Encoding.UTF8;
                    msg.UserAgent = "MyAgent/1.0";
                    msg.MethodName = "metaWeblog.newPost";
 
                    // Add blogId parameter
                    XmlRpcParameter blogId = new XmlRpcParameter();
                    blogId.ParameterType = XmlRpcParameterType.String;
                    blogId.Value = "0";
                    msg.Parameters.Add(blogId);
 
                    // Add username paramenter
                    XmlRpcParameter username = new XmlRpcParameter();
                    username.ParameterType = XmlRpcParameterType.String;
                    username.Value = "myusername";
                    msg.Parameters.Add(username);
 
                    // Add password parameter
                    XmlRpcParameter password = new XmlRpcParameter();
                    password.ParameterType = XmlRpcParameterType.String;
                    password.Value = "mypassword";
                    msg.Parameters.Add(password);
 
                    // Add struct parameter
                    XmlRpcParameter article = new XmlRpcParameter();
                    article.ParameterType = XmlRpcParameterType.Struct;
 
                    // Create struct members
                    XmlRpcStructParameter articleStruct = new XmlRpcStructParameter();
                    XmlRpcParameter title = new XmlRpcParameter();
                    XmlRpcParameter description = new XmlRpcParameter();
                    XmlRpcParameter author = new XmlRpcParameter();
                    XmlRpcParameter guid = new XmlRpcParameter();
                    XmlRpcParameter link = new XmlRpcParameter();
 
                    // Assign value to title struct member
                    title.ParameterType = XmlRpcParameterType.String;
                    title.Value = "This is my title";
                    articleStruct.Members.Add("title", title);
 
                    // Assign value to description struct member
                    description.ParameterType = XmlRpcParameterType.String;
                    description.Value = "Filling in some text so that my description has some content";
                    articleStruct.Members.Add("description", description);
 
                    // Assign value to author struct member
                    author.ParameterType = XmlRpcParameterType.String;
                    author.Value = "bbehm";
                    articleStruct.Members.Add("author", author);
 
                    // Assign the struct to the struct parameter value
                    article.Value = articleStruct;
                    msg.Parameters.Add(article);
 
                    // Add the publish parameter
                    XmlRpcParameter publish = new XmlRpcParameter();
                    publish.ParameterType = XmlRpcParameterType.Boolean;
                    publish.Value = true;
                    msg.Parameters.Add(publish);
 
                    // Send the message and look at response
                    XmlRpcResponse response;
                    response = client.Send(new Uri("http://www.mywebsite.com/xmlrpc.php"), msg);
                    if (response.HasFault)
                    {
                        int faultCode = response.FaultCode;
                        string faultReason = response.FaultReason;
                        MessageBox.Show(string.Format("Error posting: {0} ({1})", faultReason, faultCode));
                    }
                }


Apparently the way that I am dealing with the struct parameter is causing some problems. As I set the article parameter type to XmlRpcParameterType.Struct the "<struct>" tag is being added to the message payload. By creating an XmlRpcStructParameter it is also adding the "<struct>" tag. My final message payload looks like this:

<?xml version="1.0" encoding="utf-8"?>
<methodCall>
  <methodName>metaWeblog.newPost</methodName>
  <params>
    <param>
      <value>
        <string>0</string>
      </value>
    </param>
    <param>
      <value>
        <string>myusername</string>
      </value>
    </param>
    <param>
      <value>
        <string>mypassword</string>
      </value>
    </param>
    <param>
      <value>
        <struct>&lt;struct&gt;
  &lt;member&gt;
    &lt;name&gt;title&lt;/name&gt;
    &lt;value&gt;
      &lt;string&gt;This is my title&lt;/string&gt;
    &lt;/value&gt;
  &lt;/member&gt;
  &lt;member&gt;
    &lt;name&gt;description&lt;/name&gt;
    &lt;value&gt;
      &lt;string&gt;Filling in some text so that my description has some content&lt;/string&gt;
    &lt;/value&gt;
  &lt;/member&gt;
  &lt;member&gt;
    &lt;name&gt;author&lt;/name&gt;
    &lt;value&gt;
      &lt;string&gt;bbehm&lt;/string&gt;
    &lt;/value&gt;
  &lt;/member&gt;
&lt;/struct&gt;</struct>
      </value>
    </param>
    <param>
      <value>
        <boolean>1</boolean>
      </value>
    </param>
  </params>
</methodCall>

Again, thanks for any help anyone can offer.
Coordinator
Jan 16, 2008 at 4:12 PM
It appears you are incorrectly trying to add a struct to a parameter. This is probably due to a lack of some discrete documentation on my part as:

XmlRpcParameter article = new XmlRpcParameter();
article.ParameterType = XmlRpcParameterType.Struct;

IS NOT HANDLED THE SAME AS

XmlRpcStructParameter articleStruct = new XmlRpcStructParameter();

Here is the modified code:
// Publish the message
using (XmlRpcClient client = new XmlRpcClient())
{
    // Set the timeout value
    client.Timeout = TimeSpan.FromSeconds(10);
 
    // Create the message
    XmlRpcMessage msg = new XmlRpcMessage();
    msg.Encoding = Encoding.UTF8;
    msg.UserAgent = "MyAgent/1.0";
    msg.MethodName = "metaWeblog.newPost";
 
    // Add blogId parameter
    XmlRpcParameter blogId = new XmlRpcParameter();
    blogId.ParameterType = XmlRpcParameterType.String;
    blogId.Value = "0";
    msg.Parameters.Add(blogId);
 
    // Add username paramenter
    XmlRpcParameter username = new XmlRpcParameter();
    username.ParameterType = XmlRpcParameterType.String;
    username.Value = "myusername";
    msg.Parameters.Add(username);
 
    // Add password parameter
    XmlRpcParameter password = new XmlRpcParameter();
    password.ParameterType = XmlRpcParameterType.String;
    password.Value = "mypassword";
    msg.Parameters.Add(password);
 
    // Create struct parameter
    XmlRpcStructParameter articleStruct = new XmlRpcStructParameter();
 
    // Create struct members
    XmlRpcParameter title = new XmlRpcParameter();
    XmlRpcParameter description = new XmlRpcParameter();
    XmlRpcParameter author = new XmlRpcParameter();
    XmlRpcParameter guid = new XmlRpcParameter();
    XmlRpcParameter link = new XmlRpcParameter();
 
    // Assign value to title struct member
    title.ParameterType = XmlRpcParameterType.String;
    title.Value = "This is my title";
    articleStruct.Members.Add("title", title);
 
    // Assign value to description struct member
    description.ParameterType = XmlRpcParameterType.String;
    description.Value = "Filling in some text so that my description has some content";
    articleStruct.Members.Add("description", description);
 
    // Assign value to author struct member
    author.ParameterType = XmlRpcParameterType.String;
    author.Value = "bbehm";
    articleStruct.Members.Add("author", author);
 
    // Add the article parameter
    article.Value = articleStruct;
    msg.Parameters.Add(articleStruct);
 
    // Add the publish parameter
    XmlRpcParameter publish = new XmlRpcParameter();
    publish.ParameterType = XmlRpcParameterType.Boolean;
    publish.Value = true;
    msg.Parameters.Add(publish);
 
    // Send the message and look at response
    XmlRpcResponse response;
    response = client.Send(new Uri("http://www.mywebsite.com/xmlrpc.php"), msg);
    if (response.HasFault)
    {
        int faultCode = response.FaultCode;
        string faultReason = response.FaultReason;
        MessageBox.Show(string.Format("Error posting: {0} ({1})", faultReason, faultCode));
    }
}
Coordinator
Jan 16, 2008 at 4:14 PM
AS the <struct> parameter is handled as a special case in the framework, I will look into throwing an exception possibly to warn against this scenario. Thanks!
Coordinator
Feb 29, 2008 at 5:16 PM
The XML-RPC architecture and XmlRpcClient in the framework have been completely rewritten in the 2008 version to simplify how to send remote procedure calls. I plan on writing a few tutorials in the Wiki that demonstrate how to send Pingback and MetaWeblog XML-RPC calls as well.