How to use metaWeblog.newPost (xmlrpc api) properly with PHP ?

Posted by Sjne on Stack Overflow See other posts from Stack Overflow or by Sjne
Published on 2010-04-22T23:52:48Z Indexed on 2010/04/23 0:13 UTC
Read the original article Hit count: 346

Filed under:
|
|

Update: solved this problem see answer

I want to make new posts on my blog remotely with XMLRPC api and i m trying to use metaWeblog.newPost function, because it provides much features. I successfully added new posts into wordpress but i failed to post it in a defined category(categories)

i tried lots of various things but failed at end now i m using code from this site http://www.samuelfolkes.com/2009/08/posting-to-wordpress-with-php-and-xml-rpc/

after stripping down the code for my needs here's what i got and its working fine

remotepost.class.php

<?php
class remotePost
{
private $client;
private $wpURL = 'http://localhost/wp/xmlrpc.php ';
private $ixrPath = '/wp-includes/class-IXR.php';
private $uname = 'zxc';
private $pass = 'zxc';
public $postID;
function __construct($content)
{
if(!is_array($content)) throw new Exception('Invalid Argument');
include $this->ixrPath;
$this->client = new IXR_Client($this->wpURL);

$this->postID = $this->postContent($content);
}
private function postContent($content)
{
$content['description'] =  $content['description'];
if(!$this->client->query('metaWeblog.newPost','',$this->uname,$this->pass,$content,true)) throw new Exception($this->client->getErrorMessage());
return $this->client->getResponse();
}
}
?>

post.php ( you can name it whatever you want )

<?php
if(isset($_POST['submit']))
{
include "remotepost.class.php";
$content['title'] = $_POST['title'];
$content['categories'] = $_POST['category'];
$content['description'] = $_POST['description'];
try
{
$posted = new remotePost($content);
$pid = $posted->postID;
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>WordPress Poster</title>
</head>
<body>
<?php
if(isset($_POST['submit'])) echo "Posted! <a href=\"http://localhost/wp/?p=$pid\">View Post</a><br /><br />";

?>
<form enctype="multipart/form-data" method="post" action="#">
Title
<input type="text" name="title" /> <br />
Category
 <input type="text" name="category" /> <br />
Description
<input type="text" name="description" /> <br />
<input type="submit" value="Submit" name="submit" />
</form>
</body>
</html>

dont know whats wrong in this code :( ,failing to post in right directory

© Stack Overflow or respective owner

Related posts about php

Related posts about xmlrpc