如何使用PHP正确使用XML-RPC metaWeblog.newPost?

时间:2010-04-22 23:52:49

标签: php wordpress xml-rpc

我想使用XMLRPC API在我的博客上远程发布新帖子,我正在尝试使用metaWeblog.newPost函数,因为它提供了更多功能。 我成功地将新帖子添加到WordPress中,但未能将其发布到已定义的类别中。

我尝试了很多各种各样的事情,但都失败了。现在我正在使用来自this site的代码,在根据我的需要删除代码后,我得到了它并且工作正常:

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 (你可以随意命名)

<?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>

为什么此代码无法在正确的目录(类别)中发布?

1 个答案:

答案 0 :(得分:2)

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

更改为

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

它必须是一个数组,它花了我整晚,我想我已经阅读了超过200页这个大声笑,gooooogled