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);
Post Info
