【问题标题】:How to use XML-RPC metaWeblog.newPost properly with PHP?如何在 PHP 中正确使用 XML-RPC metaWeblog.newPost?
【发布时间】:2011-02-11 07:49:15
【问题描述】:

我想使用 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>

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

【问题讨论】:

    标签: php wordpress xml-rpc


    【解决方案1】:
    include "remotepost.class.php";
    $content['title'] = $_POST['title'];
    $content['categories'] = $_POST['category'];
    $content['description'] = $_POST['description'];
    

    改成

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

    一定是个数组,花了我一晚上的时间,我想我已经为这个lol阅读了200多页,gooooogled

    【讨论】:

      猜你喜欢
      • 2013-09-12
      • 2012-02-13
      • 2014-05-14
      • 2010-11-07
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多