Created
December 21, 2010 22:07
-
-
Save jhaus/750705 to your computer and use it in GitHub Desktop.
Google's cache of http://www.phpmadesimple.info/2009/07/05/using-wordpress-xmlrpc-services/ as it appeared on Sep 18, 2010
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Using Wordpress XMLRPC services Jul 5th, 2009 | By admin | Category: Featured, PHP, Tutorials | |
(1 votes, average: 4.00 out of 5) | |
Wordpress is a great blogging platform. With tons of plugins available, Wordpress is quickly become a good choice not only for personal blogging purpose but also for making content oriented sites. There are also many sites want to add ‘blog’ feature into it and WP one more time is the chosen one. Although WP’s plugin architecture is really not good, the WP’s team have added in a really interesting tool that help integration between WP blog and hosting site easier. It’s XMLRPC services. | |
Here are some scenarios if you has a WP blog and you want to integrate it with another site, you will found XMLRPC helpful: | |
Showing latest posts/comments on the site homepage | |
Provide links to categories in your blog | |
Turning your WP blog into a collaborative environment for your site’s authors | |
Off course, the 3rd scenario look very interesting especially in case your site is quite static, you need to update content by manually update HTML files or you are using some kinds of simple content management tools that do not support multiple user, authorizing process, tagging or categorizing. | |
Though in the limitation of this post, I can only show how to use WP’s XMLRPC. So, why XMLRPC why I can write direct SQL queries to get data (post, comment, category, etc.) from WP’s database - The answer is simple, do not reinvent the wheels. You can write queries for sure, but how about building friendly urls to the posts if WP change the perma link format ? Are your sure you understand the wp_terms table well enough for getting list of categories - And are you sure to handle the logic of post status, post types, total posts in a category, etc. well - Those are enough to decide that XMLRPC must be use when an external want to get something from your WP’s blog. It’s no need to mention about the situation you run your site on one server and WP on another. TURN IT ON ! XMLRPC is turn off by default, otherwise some wise users of your blog who has access to it can take your content away. So, turn it on with care. | |
You know where it’s but you might not know what it’s about. Just in case you don’t remember, go to Options > Writing page and check the option "Enable XMLRPC". | |
GET LIST OF AVAILABLE SERVICES Now you can use XMLRPC, just get list of what services you can use. | |
It’s provided on WP’s codex XML RPC page: http://codex.wordpress.org/XML-RPC | |
A SIMPLE USAGE SAMPLE view sourceprint | |
01. include_once('wp-includes/class-IXR.php'); | |
02. | |
03. define('BLOG_ID', 0); | |
04. define('RPC_USERNAME', 'admin'); | |
05. define('RPC_PASSWORD', '***'); | |
06. | |
07. $client = new IXR_Client('http://your.blog.path/xmlrpc.php'); | |
08. $client->&query('wp.getCategories', BLOG_ID, RPC_USERNAME, RPC_PASSWORD); | |
09. $categories = $client->&getResponse(); | |
10. | |
11. print_r($categories); | |
To use XMLRPC services, you must create an IRX_Client object. If you want to go in deep, the implementation code of this class is in wp-includes/class-IXR.php. The server that IRX_Client object will work with is defined by your blog’s url appended by xmlrpc.php. You must initialize the $client with this url. | |
Now, to call a service, the service name is always the first param of $client->query() following by service’s parameters. Most of the time, you need to provide BLOG_ID, username and password to login. the getResponse() method of IRX_Client object will return result of your query, normally an array as in this case, we get list of categories in our blog. | |
Simple and easy ! OK, BUT YOU DON’T SEE WHAT YOU NEED IN THE SERVICE LIST Sometimes, you don’t. Though there are many services implemented by the WP’s team, you must know somehow to extend the list. This is simple if you are familiar with some WP functions. | |
Firstly, open the xmlrpc.php file and search for ‘function wp_xmlrpc_server’. This function is a map of XMLRPC query name and the actual functions that implement the logic. | |
view sourceprint | |
01. $this->&methods = array( | |
02. // WordPress API | |
03. 'wp.getUsersBlogs' =>& 'this:wp_getUsersBlogs', | |
04. 'wp.getPage' =>& 'this:wp_getPage', | |
05. 'wp.getPages' =>& 'this:wp_getPages', | |
06. 'wp.newPage' =>& 'this:wp_newPage', | |
07. 'wp.deletePage' =>& 'this:wp_deletePage', | |
08. 'wp.editPage' =>& 'this:wp_editPage', | |
09. 'wp.getPageList' =>& 'this:wp_getPageList', | |
10. 'wp.getAuthors' =>& 'this:wp_getAuthors', | |
11. 'wp.getRecentPosts' =>& 'this:wp_getRecentPosts', | |
12. 'wp.getCategories' =>& 'this:mw_getCategories', // Alias | |
13. //Still long list of services for other blogging platforms | |
Just take a look of one service, for example, the wp.getCategories we use and then you know how to write yours. Here is a sample of getRecentPosts that I added in my XMLRPC: | |
view sourceprint | |
01. function wp_getRecentPosts($args){ | |
02. $this->&escape($args); | |
03. | |
04. $blog_ID = (int) $args[0]; | |
05. $username = $args[1]; | |
06. $password = $args[2]; | |
07. $limit = $args[3]; | |
08. | |
09. if ( !$user = $this->&login($username, $password) ) { | |
10. return $this->&error; | |
11. } | |
12. $posts = wp_get_recent_posts($limit); | |
13. foreach($posts as &$post){ | |
14. //use guid field as perma link | |
15. $plink = get_permalink($post['ID']); | |
16. $post['guid'] = $plink; | |
17. //Get author | |
18. $author = get_userdata($post['post_author']); | |
19. $post['post_author'] = $author->&user_login; | |
20. } | |
21. return $posts; | |
22. } | |
That is. I am sure that if you spend time to write all the get_userdata, get_permalink, wp_get_recent_post stuff, you will need day(s). But if you use XMLRPC and just in case you don’t work with WP usual, refer to WP Codex’s functions reference page for utility functions you need, you can get what you need in hours. | |
Tags: wordpress | |
ONE COMMENT LEAVE A COMMENT » Using Wordpress XMLRPC services | PHP made simple July 6th, 2009 7:27 am [...] Go here to see the original: Using Wordpress XMLRPC services | PHP made simple [...] LEAVE COMMENT Name (required) Mail (will not be published) (required) Website MORE FROM THIS CATEGORY Using Wordpress XMLRPC services Prototype and Scriptaculous tree Object Oriented Programming with JavaScript – Before Prototype, jQuery or ExtJS Building a Data Access layer using PDO Archive for 'Featured' » MORE FROM THIS CATEGORY Why you should choose Yii to develop your web application Using Wordpress XMLRPC services Yii, the new emerging framework for PHP Resize images with ImageMagic Writing the Hello world application with Prado Introduction to PHP Building a Data Access layer using PDO Installing Apache on Windows Using Zend components in your own web project Getting started with Prado Archive for 'PHP' » MORE FROM THIS CATEGORY Using Wordpress XMLRPC services Using XML web services Learn CSS by examples Installing Apache on Windows Using Zend components in your own web project Archive for 'Tutorials' » BROWSE CATEGORIES Featured JavaScript PHP Prado framework Tutorials Uncategorized ADS & SPONSORS BROWSE ARCHIVES August 2010 June 2010 July 2009 June 2009 September 2008 August 2008 July 2008 June 2008 April 2008 March 2008 February 2008 January 2008 December 2007 STAY INFORMED Entries (RSS) Comments RSS) WHO WRITES admin © 2010 PHP Made Simple | Powered by WordPress | BranfordMagazine theme by Michael Oeser. Based on Mimbo and Revolution Log in | 39 queries. 1.018 seconds. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment