How to get WordPress posts from a single category using XML-RPC

前端 未结 1 750
谎友^
谎友^ 2021-01-16 01:08

I\'m developing an Android app for a WordPress site. I\'m using XMLRPC to fetch posts from the server and list them in the app.

Since the XMLRPC client I use for And

相关标签:
1条回答
  • 2021-01-16 01:46

    You'll create a plugin that implements a custom function as shown in Custom XML-RPC Methods in WordPress and install it on the target site.

    <?php
    /**
     * Plugin Name: (SO) Custom XML-RPC
     * Author: brasofilo
     * Plugin URI: http://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;
        }
    }
    

    The following is a test file in PHP (I don't know 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 );
    
    0 讨论(0)
提交回复
热议问题