WordPress XMLRPC- metaWeblog.newPost
With wordpress XMLRPC, you can write your own client to blog from your desktop. metaWeblog.newPost takes five parameters
- Blog ID
- Username
- Password
- Content of the Post
- 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
- Title
- Date in ISO8601 format
- Description
- 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
... other posts by Joys of Programming
Comments:
On Facebook
Translate









Search
Custom SearchRecent Comments
- Ash on log4cpp::PatternLayout – Available log4cpp log formats
- Joys of Programming on Curl / libCurl Tutorials with Examples
- Malik89 on Curl / libCurl Tutorials with Examples
- Matt on How to install java library for jogl in Ubuntu/Linux?
- Gokulnath on How to install Readline/libreadline in Ubuntu?
On Twitter
Tutorials
Twitter
- And you thought Eclipse is Just for Java Development. You can also use Eclipse for C/C++ development http://t.co/swtOLbkr 3 months ago
- For the C fans here, let's see a simple question. Is there any difference between func() and func(void)? http://t.co/ASTsWVc9 3 months ago
- ISO C++11 released. http://t.co/DyJ0rSa1 3 months ago
- RT @newsycombinator: IE team sends a cupcake for FireFox 5 http://j.mp/lS7pGF 7 months ago
- RT @newsycombinator: Voice search enabled on Google homepage (Chrome) http://j.mp/m5KT8c 7 months ago
Categories
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
You can refer the solution for this problem here
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
nvm, I found it: wp.newPage
Here a nice reference list: http://oldtownit.com/metaPost.ashx
You can also use metaWeblog.newPost to publish a page. Refer the article here.
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.
Make sure that you have installed php-xmlrpc package. If you haven’t installed, follow the post on php5-xmlrpc installation.
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!
Thanks Ed
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);