WordPress Posting via XML-RPC
XMLRPCURL = $xmlrpcurl;
$this->UserName = $username;
$this->PassWord = $password;
}
/* Input: request name and the parameters for the request
*
* Function will then create an XML request from the input parameters
* and then send the request to the WordPress installation XML-RPC URL
* and return the results.
*
* cURL is a command line tool for getting or sending files using URL
* syntax. cURL is then used to send the request on the XML-RPC URL
* of the WordPress installation stored in the class member variable.
*/
function send_request($requestname, $params)
{
$request = xmlrpc_encode_request($requestname, $params);
print "
The XML for the RPC call
" . $request . "
";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $this->XMLRPCURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$results = curl_exec($ch);
curl_close($ch);
print "The XML for the RPC return
" . $results . "
";
return $results;
}
function sayHello()
{
$params = array();
return $this->send_request('demo.sayHello',$params);
}
function create_post($title,$body,$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,$this->UserName,$this->PassWord,$content,true);
return $this->send_request('metaWeblog.newPost',$params);
}
}
// Get posted variables
$title = $_POST["title"];
$post = $_POST["post"];
if ( !isset($title) || trim($title) === '' ) {
print "Cannot post to WordPress without a title.
";
} elseif ( !isset($post) || trim($post) === '' ) {
print "Cannot post to WordPress without a body of post.
";
} else {
// OK to post. Add some comments before post
$postBody = "";
$postBody .= "This posting was automatically generated using web services.
" . $post;
// Use method create_post from object instantiated from XMLRPClientWordPress
$objXMLRPClientWordPress = new XMLRPClientWordPress("http://jackmyers.info/blog/xmlrpc.php" , "XML" , "dummyPWD");
$result = $objXMLRPClientWordPress->create_post($title, $postBody,'XML-RPC');
// Feedback
print "Results: " . $result . "
";
print "The following post will appear on the blog site:
";
print "" . $title . "
";
print "" . $postBody . "
";
print "You can find this post here.