Skip to content

xmlrpc操作typecho

Published:

首先要在基本设置里面打开xmlrpc接口

使用aXMLRPC库来调用接口

添加引用

// build.gradle.kts
// https://mvnrepository.com/artifact/fr.turri/aXMLRPC
implementation("fr.turri:aXMLRPC:1.14.0")

初始化XMLRPCClient

val client = XMLRPCClient(URL("http://localhost/action/xmlrpc"))

文章列表

mwGetRecentPosts

    /**
     * 获取前$postsNum个post
     *
     * @param int $blogId
     * @param string $userName
     * @param string $password
     * @param int $postsNum
     * @return array
     */
    public function mwGetRecentPosts(int $blogId, string $userName, string $password, int $postsNum): array

调用

val ret = client.call("mt.getRecentPostTitles", 0, username, password, 100) as Array<Any>
val map = ret.map {
    it.toString().removeSurrounding("{", "}")
        .split(", ")
        .associate {
            val p = it.indexOf('=')
            it.substring(0, p) to it.substring(p + 1)
        }
}
// ------
map.forEach{ p ->
    p["postid"] //文章id
    p["title"] //标题
    p["dateCreated"] //创建日期
}

文章详细信息

mwGetPost

    /**
     * 获取指定id的post
     *
     * @param int $postId
     * @param string $userName
     * @param string $password
     * @return array
     */
    public function mwGetPost(int $postId, string $userName, string $password): array

调用

val ret = client.call("metaWeblog.getPost", id.toInt(), username, password) as Map<String, Any>
return PostData(
    id = ret["postid"]?.toString()?:"",
    userid = ret["userid"]?.toString()?:"",
    title = ret["title"]?.toString()?:"",
    description = ret["description"]?.toString()?:"",
    content = ret["mt_text_more"]?.toString()?:"",
    tags = ret["mt_keywords"]?.toString()?:"",
    data = ret
)

修改文章

mwEditPost

    /**
     * 编辑post
     *
     * @param int $postId
     * @param string $userName
     * @param string $password
     * @param array $content
     * @param bool $publish
     * @return int
     * @throws \Typecho\Db\Exception
     */
    public function mwEditPost(
        int $postId,
        string $userName,
        string $password,
        array $content,
        bool $publish = true
    ): int

调用

val content = HashMap<String, String>()
content["description"] = "文章内容"
content["userid"] = userid
content["postid"] = postid
content["title"] = "标题"
val ret = client.call("metaWeblog.editPost", postid, username, password, content, true)

发布文章

mwNewPost

    /**
     * MetaWeblog API
     * about MetaWeblog API, you can see http://www.xmlrpc.com/metaWeblogApi
     *
     * @param int $blogId
     * @param string $userName
     * @param string $password
     * @param array $content
     * @param bool $publish
     * @return int
     * @throws \Typecho\Db\Exception
     */
    public function mwNewPost(int $blogId, string $userName, string $password, array $content, bool $publish): int

调用

val content = HashMap<String, String>()
content["description"] = "文章内容"
content["userid"] = ""
content["postid"] = "0"
content["title"] = "标题"
val ret = client.call("metaWeblog.newPost", 0, username, password, content, true)

Previous Post
电脑HDMI接电视颜色泛白
Next Post
网易云时间