XMLRPC: blogger.getPost

WordPress XMLRPC also supports blogger API. blogger.getPost helps you to get the contents of a Post.

blogger.getPost takes the following parameters

  1. appkey: ignored (pass some blank value)
  2. postid: the id of the post
  3. username,
  4. password
  5. to publish or not (boolean-optional)

The

<?php

/*The URL of the blog*/
$BLOGURL = "http://localhost/wordpress";

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

    /*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 blogger.getPost request which takes on five parameters
 appkey: ignored (pass some blank value)
 postid: the id of the post
 username,
 password
 to publish or not*/
function blogger_getPost($appkey,$postid, $username, $password, $toPublish) {
   global $BLOGURL;
   $request = xmlrpc_encode_request("blogger.getPost",
      array($appkey, $postid,$username, $password,  $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);
}

$appkey="";

$USERNAME = "admin";
$PASSWORD = "check";
/*The id of the post to be edited*/
$postid = 100;

/*Whether the post has to be published, usually ignored*/
$toPublish = true;

blogger_getPost($appkey, $postid, $USERNAME, $PASSWORD, $toPublish);
echo "n";
?>

Let’s assume that we send a non existent post id as input, You will find that you get some blank contents
[xml]
$ php blogger.getPost.php

<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member><name>userid</name><value><string></string></value></member>
<member><name>dateCreated</name><value><dateTime.iso8601>T::</dateTime.iso8601></value></member>
<member><name>content</name><value><string>&lt;title&gt;&lt;/title&gt;&lt;category&gt;&lt;/category&gt;</string></value></member>
<member><name>postid</name><value><string></string></value></member>
</struct>
</value>
</param>
</params>
</methodResponse>
[/xml]

Now let’s try with an existent Post ID
[xml]
$ php blogger.getPost.php

<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member><name>userid</name><value><string>1</string></value></member>
<member><name>dateCreated</name><value><dateTime.iso8601>20100916T00:40:41</dateTime.iso8601></value></member>
<member><name>content</name><value><string>&lt;title&gt;Hello world!&lt;/title&gt;&lt;category&gt;1&lt;/category&gt;Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!</string></value></member>
<member><name>postid</name><value><int>1</int></value></member>
</struct>
</value>
</param>
</params>
</methodResponse>
[/xml]

As you can see that you find all the contents properly filled- the title, date of creation, the contents of the post

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s