Post to Wordpress with PHP
The ability to post to a wordpress blog from any server opens up many possibilities. We can do this with relative ease by using the wordpress XMLRPC API which is built in to every wordpress blog. The following PHP script has been tested with the latest stable version of wordpress (2.8.6) and should work from any webserver with PHP & cURL capability. You must enable the WordPress, Movable Type, MetaWeblog and Blogger XML-RPC publishing protocols under the reading section of wp-admin. This script is released under the GNU General Public License.
Post To Wordpress XMLRPC API with PHP
$title = 'This is the post title';
$body = 'this is the post content';
$rpcurl = 'http://www.yourwordpressblog.com/xmlrpc.php';
$username = 'myusername';
$password = 'mypassword';
$category = ''; //default is 1, enter a number here.
$keywords = 'one,two,three';//keywords comma seperated.
$encoding ='UTF-8';//utf8 recommended
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords='',$encoding='UTF-8')
{
$title = htmlentities($title,ENT_NOQUOTES,$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding);
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category)
);
$params = array(0,$username,$password,$content,true);
$request = xmlrpc_encode_request('metaWeblog.newPost',$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);
return $results;
}
wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$category,$keywords,$encoding);
(Noreply, 2009)
This script is easy to configure by simply replacing the example values given in the first lines with some actual data. (The link to xmlrpc.com should be www.yourdomain.com/xmlrpc.php if your blog resides in the root directory.) Put the whole thing into a PHP file, and run it. (Pekka. 2010)
Short explanation of the Parameters in the script
$title = 'This is the post title';
$body = 'this is the post content';
$rpcurl = 'http://www.yourwordpressblog.com/xmlrpc.php';
$username = 'myusername';
$password = 'mypassword';
$category = ''; //default is 1, enter a number here.
$keywords = 'one,two,three';//keywords comma seperated.
$encoding ='UTF-8';//utf8 recommended
- 1. $title: The title of the article
- 2. $body: The body of the article in HTML
- 3. $rpurl: The url of the XMLRPC api of your blog, in general http://blog_url/xmlrpc.php
- 4. $username: The username that will post (must have permission to post)
- 5. $password: The password of the username posting
- 6. $category: The name of the catagory in which the post will be classified
- 7. $keywords: The tags (key words) associated with the article in the form of a comma seperated list
- 8. $encoding: The encoding, by default UTF-8 but you can change it to the parameters of your blog
(Noreply, 2009)
Posting Multiple Items
If you want to post several items, there are only a few things that change for every post:
- 1. The title of the post.
- 2. The contents of the post
- 3. The category of the post
- 4. The keywords (optional).
The RPC URL, username, password and encoding is standard if you’re posting it onto the same website the script resides on. So we only have to store the 4 named items in an array that we can run through. In the following example we store the items in another array, so we have an array of arrays.
You could write something like this:
// We create a post array that contains an array per post.
// We put in the data index based.
// First item is title,
// Second is content body,
// Third is category, fourth is keywords.
$posts = array(
array('Title','Contents','category','keywords'),
array('Another post','More content','another category ID','etc.'),
// You can add more items if you want.
);
// These are just general settings that are the same for each post.
$rpcurl = 'http://www.yourwordpressblog.com/xmlrpc.php';
$username = 'myusername';
$password = 'mypassword';
$encoding ='UTF-8';
foreach($posts AS $Post)
{
// From the foreach we get an array with post data
// each cycle. To keep things a bit clear,
// We will extract the data to seperate variables..
$title = $Post[0];
$body = $Post[1];
$category = $Post[2];
$keywords = $Post[3];
wpPostXMLRPC($title,$body,$rpcurl,$username,
$password,$category,$keywords,$encoding);
}
(Hans, 2010)

(5 votes, average: 4.20 out of 5)