2010-04-22 79 views
1

我想使用XMLRPC API在我的博客上远程创建新帖子,我试图使用metaWeblog.newPost函数,因为它提供了更多功能。 我成功将新帖子添加到WordPress中,但未能将其发布到指定的类别中。如何正确使用XML-RPC metaWeblog.newPost?

我尝试了很多各种各样的东西,但都失败了。现在,我使用的代码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> 

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

回答

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

变化

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

它必须是一个数组,它带走了我所有的夜晚,我想我已经阅读200多页这个笑,gooooogled