java-如何使用XML-RPC从单个类别获取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