WordPress XMLRPC- metaWeblog.newPost

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

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

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

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

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 five parameters
   blogid,
   username,
   password*/

  /*The title of your post*/
  $title = "Sample Post Title";

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

  /*Forming the content of blog post*/
  $content['title'] = $title;
  $content['description'] = $description;
  $content['categories'] = array("Blogging", "category1");
  /*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>19</string>
      </value>
    </param>
  </params>
</methodResponse>

Executing the above program will create multiple posts

Tags: , ,

Comments:

10 Comments

  • I used
    $content['dateCreated'] = ’2010-08-19T12:05:10′;

    But error:
    Fatal error: Call to a member function getIso() on a non-object in /home/abhishek/public_html/blog/xmlrpc.php on line 2151

  • Joys of Programming says:

    You can refer the solution for this problem here

  • Claudio says:

    Hi, do you know if it’s also possible to publish Pages via Metaweblog? With your code, I only can publish Posts, which is already cool ;)

  • Claudio says:

    nvm, I found it: wp.newPage
    Here a nice reference list: http://oldtownit.com/metaPost.ashx

  • Joys of Programming says:

    You can also use metaWeblog.newPost to publish a page. Refer the article here.

  • Ed Gerstner says:

    Been tearing my hair out trying to get this to work. I’ve copied and pasted the above code into test.php and inserted all the relevant information for it to access a local sandbox installation of my wordpress blog. My problem seems to be that things stop at the first xmlrpc_encode_request call. I’ve tried testing the existence of xmlrpc_encode_request in my installation with

    if(!function_exists(‘xmlrpc_encode_request’)) {die (“Xmlrpc_encode_request not installed\n”); }

    and indeed it seems it doesn’t. I’m using the lastest version of MAMP (php 5.3) on a MacBook Pro. Any thoughts? Thank you.

  • Joys of Programming says:

    Make sure that you have installed php-xmlrpc package. If you haven’t installed, follow the post on php5-xmlrpc installation.

  • Ed Gerstner says:

    Aha! Thanks for your lightning-fast response. Yup, of course that’s the problem. *slaps forehead*

    It took me a while to work out how to add it to MAMP. If anyone else happens across this and wants to know, I eventually found the solution here.

    Thanks again! And kudos for the site!

  • Joys of Programming says:

    Thanks Ed

  • Marc Quimby says:

    I did a regex on the request variable to change the date value from string to dateTime.iso8601. This was done in PHP.

    $request = xmlrpc_encode_request('metaWeblog.newPost',$params);
    $request = preg_replace("/(\d*T\d\d:\d\d:\d\d)/", "\\1", $request);

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.