WordPress XMLRPC- Publishing a page using metaWeblog.newPost

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 six parameters

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

The fifth parameter (Post type) can be used to publish a page. If this parameter is not set, a blog post is taken as the default parameter.But if it is set to ‘page’, a page will be published. The sixth 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

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 the ID of the post.

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.newPost request which takes on parameters
   blogid,
   username,
   password
   content
   post_type
   to publish or not*/

  /*The title of your page*/
  $title = "Sample Page";

  /*The contents of your page*/
  $description = "This is a sample page.";

  /*Forming the content of page*/
  $content['title'] = $title;
  $content['post_type'] = 'page'; /*This is a page*/
  $content['description'] = $description;
  $content['wp_slug'] = "sample-page";
  /*Whether the post has to be published*/
  $toPublish = true;
  $request = xmlrpc_encode_request("metaWeblog.newPost",
    array(1,$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.newPost.php

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

<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value>
        <string>44</string>
      </value>
    </param>
  </params>
</methodResponse>

Executing the above program multiple times will create multiple posts

Tags: , , , ,

Comments:

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.