How to use XML-RPC metaWeblog.newPost properly with PHP?

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 more features.
I successfully added new posts into WordPress but failed to post it in a defined category.

I tried lots of various things but failed. Now I’m using code from this site, after stripping down the code for my needs here’s what I got and it’s working fine:

Read More

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>

Why this code fails to post in right directory (categories)?

Related posts

Leave a Reply

1 comment

  1. include "remotepost.class.php";
    $content['title'] = $_POST['title'];
    $content['categories'] = $_POST['category'];
    $content['description'] = $_POST['description'];
    

    changes to

    $content['categories'] = array($_POST['category']); 
    

    It must be an array, it took my all night, i guess i have read more than 200 pages for this lol, gooooogled