ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-如何使用XML-RPC从单个类别获取WordPress帖子

2019-10-09 09:14:17  阅读:993  来源: 互联网

标签:xml-rpc android java wordpress


我正在为WordPress网站开发一个Android应用程序.我正在使用XMLRPC从服务器中获取帖子,并在应用程序中列出它们.

由于我用于Android的XMLRPC客户端使我能够调用WordPress的方法并处理结果,因此我完全依赖WordPress的class-wp-xmlrpc-server.php文件中可用的方法.

到目前为止,我使用了WordPress XMLRPC API中的wp.getPosts()方法,并且成功获取了最新的帖子,如下所示:

Object[] params = {1, username, password}; // parameters for wp.getPosts(), blogID (1), username and password.

XMLRPCClient client = new XMLRPCClient(url, username, password);

Object[] posts = client.call("wp.getPosts", params);

问题:我希望能够获得最新帖子,但只能从一个类别获得,而不是像上面那样从整个网站(所有类别)获得.我搜索了[WordPress XMLRPC API文档] [3],但找不到任何可以传递给wp.getPosts()的参数,该参数仅允许我从一个特定类别中检索帖子.

我觉得有一个简单的解决方案,但是我似乎错过了.

解决方法:

您将创建一个实现自定义功能的插件,如Custom XML-RPC Methods in WordPress所示,并将其安装在目标站点上.

<?php
/**
 * Plugin Name: (SO) Custom XML-RPC
 * Author: brasofilo
 * Plugin URI: https://stackoverflow.com/a/25507463/1287812
 */

add_filter( 'xmlrpc_methods', 'add_my_xmlrpc_methods' );

function add_my_xmlrpc_methods( $methods ) {
    $methods['b5f.getPosts'] = 'b5f_xmlrpc_get_posts';
    return $methods;
}

function b5f_xmlrpc_get_posts( $args ) {
    global $wp_xmlrpc_server;
    $wp_xmlrpc_server->escape( $args );

    $blog_ID     = (int) $args[0];
    $username  = $args[1];
    $password   = $args[2];
    $post_type  = $args[3];
    $category = $args[4];
    $numberposts = $args[5];
    $extra = $args[6];

    if ( !$user = $wp_xmlrpc_server->login($username, $password) ) {
        return $wp_xmlrpc_server->error;
    }

    $category_int = (int) $category;

    if( !empty( $category_int ) ) {
        $category_call = 'cat';
    } else {
        $category_call = 'category_name';
    }

    $post_args = array(
        'numberposts' => $numberposts,
        'posts_per_page' => $numberposts,
        $category_call => $category,
        'post_type' => $post_type,
        'post_status' => 'any'
    );
    if( is_array( $extra ) )
        $post_args = array_merge( $post_args, $extra );

    $posts_list = get_posts( $post_args );

    if ( !$posts_list ) {
        $wp_xmlrpc_server->error = new IXR_Error(500, __('Either there are no posts, or something went wrong.'));
        return $wp_xmlrpc_server->error;
    } else {
        return $posts_list;
    }
}

以下是PHP(我不知道Java)中的测试文件:

<?php
/**
 * Auxiliary function: prints the main node from the returned value
 */
function print_xml_node( $string )
{
    $xml = simplexml_load_string( $string );
    printf( '<pre><code>%s</code></pre>', print_r( $xml->params->param->value->array->data->value, true ) );
}

/**
 * XML-RPC call
 */
function query_last_posts( $methodName, $url, $parameters )
{
    $request = xmlrpc_encode_request( $methodName, $parameters );
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $request );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    curl_setopt( $ch, CURLOPT_TIMEOUT, 1 );
    $results = curl_exec( $ch );
    curl_close( $ch );
    return $results;
}

$blog_id = 1;
$user = 'username';
$pass = 'password';
$type = 'post';
$category = 1; // ID or category name
$num_posts = 1;
$args = array( $blog_id, $user, $pass, $type, $category, $num_posts, null );
$response = query_last_posts( 'b5f.getPosts', 'http://EXAMPLE.COM/xmlrpc.php', $args );

print_xml_node( $response );

标签:xml-rpc,android,java,wordpress
来源: https://codeday.me/bug/20191009/1878223.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有