Posting to Wordpress Remotely in PHP

Status
Not open for further replies.

nickycakes

Banned
Sep 28, 2007
3,444
73
0
San Diego - I work for Smaxor now
I put this on my blog, but I'm sure plenty of people don't read it so here it is. I couldn't find a simple php script to post to wordpress with it's XML-RPC server, so I made my own:

Code:
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$categories=array(1)){
  $categories = implode(",", $categories);
  $XML = "<title>$title</title>".
         "<category>$categories</category>".
         $body;
  $params = array('','',$username,$password,$XML,1);
  $request = xmlrpc_encode_request('blogger.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);
  curl_exec($ch);
  curl_close($ch);
}

$rpcurl is the wp xml-rpc server, usually yourblog.com/xmlrpc.php and $categories is an array containing the categories you want it posted to, defaults to uncategorized
 


You'll need to use the "metaWeblog.newPost" method if you want to control the publishing date of the post. In that case, it's be better to use a true XML-RPC client so you can pass the date in ISO format with a properly declared value type.

XML-RPC for PHP
 
Depending on the volume it may be more efficient to post directly to the database.
It takes some effort to map out all the fields but it's doable.
 
Status
Not open for further replies.