WordPress XMLRPC- metaWeblog.editPost

Posted by Joys of Programming on in Wordpress, XML-RPC

With wordpress XMLRPC, you can write your own client to blog from your desktop. metaWeblog.newPost takes five parameters

  1. Post ID
  2. Username
  3. Password
  4. Content of the Post
  5. To Publish or not

The first parameter is the ID of the post to be edited. The fourth parameter contains the content of the post (edited post).The fifth parameter is a boolean value, true or false, whether to publish the post or not

The fourth argument, contents of a blog must have the following attributes

  1. Title
  2. Date in ISO8601 format
  3. Description
  4. Categories
  5. Tags

You can change the title, date, description, categories or tags. Here we demonstrate how to change the contents
Note that the categories must be an existing one. You cannot create a new category with this function. The return value of this function is a boolean value whether the post is posted or not. Let’s first try with an invalid post ID

Let’s see a simple PHP program

<?php

$BLOGURL = "http://localhost/wordpress";
$USERNAME = "admin";
$PASSWORD = "check";

function get_response($URL, $context) {
 if(!function_exists('curl_init')) {
 die ("Curl PHP package not installed\n");
 }

 /*Initializing CURL*/
 $curlHandle = curl_init();

 /*The URL to be downloaded is set*/
 curl_setopt($curlHandle, CURLOPT_URL, $URL);
 curl_setopt($curlHandle, CURLOPT_HEADER, false);
 curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
 curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $context);

 /*Now execute the CURL, download the URL specified*/
 $response = curl_exec($curlHandle);
 return $response;
}

  /*Creating the metaWeblog.editPost request which takes on five parameters
   postid: the post to be edited,
   username,
   password
   contents
   to publish or not*/

  /*The id of the post to be edited*/
  $postid = 140;

  /*The title of your post*/
  $title = "Sample Post Title";
  /*The contents of your post (The new contents or the edited post)*/
  $description = "This is the contents of the edited post.";

  /*Forming the content of blog post*/
  $content['title'] = $title;
  $content['description'] = $description;
  $content['categories'] = array("Blogging", "category1");
  $content['mt_keywords'] = array("Blogging", "tag1");
  $content['wp_slug'] = "sample-post";
  /*Whether the post has to be published*/
  $toPublish = true;
  $request = xmlrpc_encode_request("metaWeblog.editPost",
    array($postid,$USERNAME, $PASSWORD, $content, $toPublish));

  /*Making the request to wordpress XMLRPC of your blog*/
  $xmlresponse = get_response($BLOGURL."/xmlrpc.php", $request);
  $response = xmlrpc_decode($xmlresponse);

  /*Printing the response on to the console*/
  print_r($response);
echo "\n";
?>

To execute the program

$ php metaWeblog.editPost.php

The output will be something like this. Note the post ID.

<?xml version="1.0"?>
<methodResponse>
  <fault>
    <value>
      <struct>
        <member>
          <name>faultCode</name>
          <value><int>404</int></value>
        </member>
        <member>
          <name>faultString</name>
          <value><string>Invalid post ID.</string></value>
        </member>
      </struct>
    </value>
  </fault>
</methodResponse>

As you can see the error response: Invalid post ID. Let’s now try the same program with a valid post ID.

$postid = 40;


The above number corresponds to a post already posted
Executing the above program with the new post ID

<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value>
        <boolean>1</boolean>
      </value>
    </param>
  </params>
</methodResponse>

See the boolean value set to true, which corresponds to the post successfully edited.

Tags: , , , ,

Comments:

5 Comments

  • ruby says:

    this is very helpful and clear document.

  • Joys of Programming says:

    Thanks ruby that you found this post useful

  • David says:

    What a great series of articles! I’ve been struggling to find this information in a clear and concise format, and you’ve done a brilliant job!

  • Joys of Programming says:

    @David Thanks that you found this article useful. You can also look at the other Tutorials

  • dan says:

    awesome! thanks for the helpful instructions.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2009-2012 Joys of Programming All rights reserved.
Desk Mess Mirrored v1.8.1 theme from BuyNowShop.com.