java利用xml-rpc协议操作wordpress博客

戚学
2023-12-01

wordpress遵循xml-rpc协议,如果在wordpress中打开rpc协议,那么你就可以利用工具或者程序发表文章。

wordpress官方rpc协议文档:XML-RPC wp « WordPress Codex

metaWeblog协议文档:https://www.xmlrpc.com/metaWeblogApi

本文参考文档:IBM Developer

发表博客代码:

public static void post(String title, String content) {
        try {
            // Set up XML-RPC connection to server
            String domain = "wangjun.easymorse.com";// 你网站的域名
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL("https://" + domain + "/xmlrpc.php"));
            XmlRpcClient client = new XmlRpcClient();
            client.setConfig(config);
            Map post = new HashMap();
            post.put("title", title);// 标题
            post.put("mt_keywords", "java");// 标签
            Object[] categories = new Object[] { "计算机" };// 分类
            post.put("categories", categories);
            post.put("description", content);// 内容
            Object[] params = new Object[] { "1", "username", "password",post,true }; // 1代表正式发布,0代表草稿
            String ob = (String) client.execute("metaWeblog.newPost", params);
            System.out.println("Created with blogid " + ob);
        } catch (Exception e) {
            System.out.println(" UnCreated " + e.getMessage());
        }
    }
    public static void main(String[] args) {
        PostBlog.post("发布测试博客","测试成功");
    }

结果:

得到分类列表代码:

public static void getBlogCategories()  {
        try{
            String domain = "wangjun.easymorse.com";//你网站的域名
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL("https://"+domain+"/xmlrpc.php"));
            XmlRpcClient client = new XmlRpcClient();
            client.setConfig(config);
            Object[] params = new Object[] {"技术分享", "username", "password" };
            // 得到分类列表
            Object[] ob=(Object[])client.execute("metaWeblog.getCategories", params);
            System.out.println("分类列表:"+ob.length+","+ob[1]);
        }catch (Exception e) {
                       System.out.println(" UnCreated "+e.getMessage());
        }
    }
    public static void main(String[] args) {
        GetBlogCategories.getBlogCategories();
    }

结果如下:

分类列表:3,{categoryName=生活, parentId=0, htmlUrl=https://wangjun.easymorse.com/?cat=25, categoryDescription=, description=生活, categoryId=25, rssUrl=https://wangjun.easymorse.com/?feed=rss2&cat=25}

得到已经发表的博客文章代码:

public static void post()  {
        try{
            String domain = "wangjun.easymorse.com";//你网站的域名
            XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
            config.setServerURL(new URL("https://"+domain+"/xmlrpc.php"));
            XmlRpcClient client = new XmlRpcClient();
            client.setConfig(config);
            Object[] params = new Object[] {"485", "wangjun", "password",10};
            Object[] ob=(Object[])client.execute("metaWeblog.getRecentPosts", params);
        System.out.println("得到博客 " + ob[7]);
        }catch (Exception e) {
                       System.out.println(" UnCreated "+e.getMessage());
        }
    }
    public static void main(String[] args) {
        GetBlog.post();
    }

结果:

得到博客 {custom_fields=[Ljava.lang.Object;@18fe7c3, postid=357, mt_allow_comments=1, permaLink=https://wangjun.easymorse.com/?p=357, post_status=publish, link=https://wangjun.easymorse.com/?p=357, mt_excerpt=, userid=1, mt_text_more=, mt_allow_pings=1, mt_keywords=android, java, milestone, title=android获取手机型号和系统版本号, date_created_gmt=Thu Jun 10 02:52:04 CST 2010, wp_password=, description=<p> 有时候需要统计手机的型号和版本号,利用程序可以获取到相应的手机信息,见:</p>  <p><a href="https://wangjun.easymorse.com/wp-content/uploads/2010/06/image3.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="81" alt="image" src="https://wangjun.easymorse.com/wp-content/uploads/2010/06/image_thumb3.png" width="244" border="0" /></a> </p>  <p>这是我手机milstone的相关信息。</p>  <p>获取的方法是:</p>  <blockquote>   <p>public void onCreate(Bundle savedInstanceState) {     <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; super.onCreate(savedInstanceState);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; setContentView(R.layout.main);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; TextView textView = (TextView) findViewById(R.id.text);      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; textView.setText(&quot;Product Model: &quot; + android.os.Build.MODEL + &quot;,&quot;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + android.os.Build.VERSION.SDK + &quot;,&quot;      <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; + android.os.Build.VERSION.RELEASE);      <br />&#160;&#160;&#160; }</p></blockquote>, dateCreated=Thu Jun 10 10:52:04 CST 2010, categories=[Ljava.lang.Object;@13e8d89, wp_author_id=1, wp_slug=android%e8%8e%b7%e5%8f%96%e6%89%8b%e6%9c%ba%e5%9e%8b%e5%8f%b7%e5%92%8c%e7%b3%bb%e7%bb%9f%e7%89%88%e6%9c%ac%e5%8f%b7, wp_author_display_name=王军}

其中发表图片方法见参考文档。

源代码:https://easymorse.googlecode.com/svn/trunk/rpctest/

 类似资料: